有什么方法可以将外部文件 "information" 保存到 java 中的数组中?
Is there any ways to save external file "information" into array in java?
例如:(我取了外部文件的第一行)
Giant, Colgate Toothpaste, 4.50
我想将它们分开并保存在这样的数组中before/after我将它们发送到对象和 ArrayList。
mall[i] = "Giant";
product[i] = "Colgate Toothpaste";
price[i] = 4.50
p/s:我觉得我应该这样做,因为我以后需要改变价格。
这就是我的编码现在的样子。
public static void readFile(ArrayList<Product> productList) throws Exception {
try {
productList.clear(); //clear the list! or remove all elements from the list!
// Coding Here
}
catch(Exception e) { System.err.println(e.getMessage());}
}
以下是“product.in”文件(外部文件)的内容
Giant, Colgate Toothpaste, 4.50
Giant, Dashing Deodorant, 6.55
Giant, Adidas Deodorant, 7.55
Giant, Dettol Hand-sanitiser, 10.00
Giant, Sokubutso Shower Foam, 15.00
Tesco, Colgate Toothpaste, 4.55
Tesco, Dettol Hand-sanitiser, 7.00
Tesco, Sokubutso Shower Foam, 15.05
Tesco, Adidas Deodorant, 7.45
Tesco, Dashing Deodorant, 5.45
TF, Sokubutso Shower Foam, 15.05
TF, Dettol Hand-sanitiser, 9.50
TF, Adidas Deodorant, 8.55
TF, Dashing Deodorant, 7.55
TF, Colgate Toothpaste, 5.00
如果您认为我提供给您的信息较少,请回复此主题。我会提供更多。
edited: add product class
class Product {
private String store;
private String item;
private double price;
public Product(String store, String item, double price) {
this.setStore(store);
this.setItem(item);
this.setPrice(price);
}
没有额外库(如 OpenCSV 或类似库)的简单实现是
- 使用
BufferedReader
和 try-with-resources
逐行读取文件以确保文件资源在处理时自动关闭。
- 使用
String.split
将每一行拆分为列
- 从列中创建一个
Product
项并将其添加到列表中
- Return结果列表。
旁注:最好使用 int
美分的价格而不是双精度价格,因为已知浮点运算不精确。
import java.io.*;
import java.util.*;
// ...
public static List<Product> readFile(String csvFile) throws Exception {
List<Product> result = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
String line;
while((line = br.readLine()) != null) {
String[] cols = line.split("\s*,\s*"); // split by comma and optional spaces
assert cols.length > 2; // make sure the line contains at least 3 columns
Product product = new Product(cols[0], cols[1], Double.parseDouble(cols[2]));
result.add(product);
}
}
catch(Exception e) {
System.err.println(e.getMessage());
throw e;
}
return result;
}
例如:(我取了外部文件的第一行)
Giant, Colgate Toothpaste, 4.50
我想将它们分开并保存在这样的数组中before/after我将它们发送到对象和 ArrayList。
mall[i] = "Giant";
product[i] = "Colgate Toothpaste";
price[i] = 4.50
p/s:我觉得我应该这样做,因为我以后需要改变价格。
这就是我的编码现在的样子。
public static void readFile(ArrayList<Product> productList) throws Exception {
try {
productList.clear(); //clear the list! or remove all elements from the list!
// Coding Here
}
catch(Exception e) { System.err.println(e.getMessage());}
}
以下是“product.in”文件(外部文件)的内容
Giant, Colgate Toothpaste, 4.50
Giant, Dashing Deodorant, 6.55
Giant, Adidas Deodorant, 7.55
Giant, Dettol Hand-sanitiser, 10.00
Giant, Sokubutso Shower Foam, 15.00
Tesco, Colgate Toothpaste, 4.55
Tesco, Dettol Hand-sanitiser, 7.00
Tesco, Sokubutso Shower Foam, 15.05
Tesco, Adidas Deodorant, 7.45
Tesco, Dashing Deodorant, 5.45
TF, Sokubutso Shower Foam, 15.05
TF, Dettol Hand-sanitiser, 9.50
TF, Adidas Deodorant, 8.55
TF, Dashing Deodorant, 7.55
TF, Colgate Toothpaste, 5.00
如果您认为我提供给您的信息较少,请回复此主题。我会提供更多。
edited: add product class
class Product {
private String store;
private String item;
private double price;
public Product(String store, String item, double price) {
this.setStore(store);
this.setItem(item);
this.setPrice(price);
}
没有额外库(如 OpenCSV 或类似库)的简单实现是
- 使用
BufferedReader
和try-with-resources
逐行读取文件以确保文件资源在处理时自动关闭。 - 使用
String.split
将每一行拆分为列 - 从列中创建一个
Product
项并将其添加到列表中 - Return结果列表。
旁注:最好使用 int
美分的价格而不是双精度价格,因为已知浮点运算不精确。
import java.io.*;
import java.util.*;
// ...
public static List<Product> readFile(String csvFile) throws Exception {
List<Product> result = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
String line;
while((line = br.readLine()) != null) {
String[] cols = line.split("\s*,\s*"); // split by comma and optional spaces
assert cols.length > 2; // make sure the line contains at least 3 columns
Product product = new Product(cols[0], cols[1], Double.parseDouble(cols[2]));
result.add(product);
}
}
catch(Exception e) {
System.err.println(e.getMessage());
throw e;
}
return result;
}