【发布时间】:2013-06-01 05:38:34
【问题描述】:
我有以下从 main 调用的代码。 代码的麻烦,它节省了产品 如下: 1,ipad,499.0,电子
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());
}
}
【问题讨论】:
-
一个有用的练习是,当您遇到此类错误时,在脑海中仔细检查您的代码,以了解它在每一步中所做的事情。这样做,错误就很明显了。
-
我会记住的.. 你们让每个人都成为更好的程序员:)
标签: java arraylist bufferedwriter