我有以下从 main 调用的代码。代码的麻烦,它保存产品如下:1,ipad,499.0,ELECTRONICS
1,ipad,499.0,电子 2,Java 电子书,19.99,BOOK
我不明白第一个来自哪里。你能给我们一些建议吗?
非常感谢...
public void saveProductsToDisk() {
String filename = "/Users/paddy/UCSC/Workspace/productDB/src/productdb/savedProducts.csv";
BufferedWriter output = null;
try
{
output = new BufferedWriter(new FileWriter(filename));
StringBuffer line = new StringBuffer();
for (Product p: getAllProducts())
{
line.append(p.getId() <=0 ? "" : p.getId());
line.append(CSV_SEPARATOR);
line.append(p.getName().trim().length() == 0? "" : p.getName());
line.append(CSV_SEPARATOR);
line.append(p.getPrice() < 0 ? "" : p.getPrice());
line.append(CSV_SEPARATOR);
line.append(p.getDept().toString());
line.append("\n");
output.write(line.toString());
}
output.flush();
output.close();
}
catch (IOException ex)
{
System.out.println("IO error for " + filename +
": " + ex.getMessage());
}
}