我需要获取一个对象列表并将它们的实例变量写入文本文件。它看起来像这样:
Hot Dog,1.25,Grocery Store
Gas,42.15,Gas Station
etc.
我有一些看起来像这样的代码:
public void writeListToFile(String fileName, ArrayList<BudgetItem> writeList) throws Exception {
PrintWriter out = null;
for(int i = 0; i<writeList.size(); i++) {
if(writeList.get(i) instanceof Expense) {
Expense writeExpense = (Expense) writeList.get(i);
try {
out = new PrintWriter(new FileWriter(fileName));
dump(out, writeExpense);
}
finally {
}
}
else if(writeList.get(i) instanceof Income) {
Income writeIncome = (Income) writeList.get(i);
try {
out = new PrintWriter(new FileWriter(fileName));
dump(out, writeIncome);
}
finally {
}
}
}
out.close();
}
public void dump(PrintWriter out, Expense writeExpense) {
out.print(writeExpense.getDateOfTransaction().get(GregorianCalendar.YEAR));
out.print(",");
out.print(writeExpense.getDateOfTransaction().get(GregorianCalendar.MONTH));
out.print(",");
out.print(writeExpense.getDateOfTransaction().get(GregorianCalendar.DATE));
out.print(",");
out.print(writeExpense.getItemName());
out.print(",");
out.print(writeExpense.getMethodOfPay());
out.print(",");
out.print(writeExpense.getPlaceOfPurchase());
out.print(",");
out.print(writeExpense.getQuantity());
out.print(",");
out.print(writeExpense.getPrice());
out.print("\n");
}
and one other method similar to the 2nd one.
当我运行它时,它只写出一行,列表中的第一个对象,没有别的。我无法弄清楚发生了什么。我知道对象序列化是一个更快的选择,但对于这个项目,因为我还在学习,我想用这种方式。
其中一个答案要求的主要方法:
public static void main(String[] args) throws Exception {
String itemName = "Hot Dog";
int quantity = 1;
String placeOfPurchase = "Weiner Stand";
String methodOfPay = "Credit";
BigDecimal price = new BigDecimal(1.25);
GregorianCalendar g = new GregorianCalendar(2013,11,1);
Expense e = new Expense(g, price, itemName, quantity, placeOfPurchase, methodOfPay);
BudgetItem bi = (BudgetItem) e;
String itemName2 = "Gun";
int quantity2 = 1;
String placeOfPurchase2 = "Weiner Stand";
String methodOfPay2 = "Credit";
BigDecimal price2 = new BigDecimal(1.25);
GregorianCalendar g2 = new GregorianCalendar(2013,11,1);
Expense e2 = new Expense(g, price, itemName, quantity, placeOfPurchase, methodOfPay);
BudgetItem bi2 = (BudgetItem) e2;
ArrayList<BudgetItem> abi = new ArrayList<BudgetItem>();
abi.add(bi);
abi.add(bi2);
RegisterFileIO rfio = new RegisterFileIO();
rfio.writeListToFile(System.getProperty("user.dir") + "/data.out", abi);
BufferedReader in = new BufferedReader(new FileReader(System.getProperty("user.dir") + "/data.out"));
Scanner lineScanner = new Scanner(in);
lineScanner.useDelimiter(",");
while(lineScanner.hasNext()) {
System.out.println(lineScanner.next());
}
}
答案 0 :(得分:2)
我认为问题是你每次迭代都会创建一个新的PrintWriter
。你应该在循环之外声明它。发生的事情是,当创建新的PrintWriter
时,它会覆盖存储在文件中的先前数据。
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter(fileName));
for(int i = 0; i<writeList.size(); i++) {
if(writeList.get(i) instanceof Expense) {
Expense writeExpense = (Expense) writeList.get(i);
dump(out, writeExpense);
}
} finally {
}
答案 1 :(得分:0)
这是因为您为列表中的每个对象实例化了一个新的PrintWriter
对象(和一个新的FileWriter
对象)。
您应该在for
循环之前仅实例化一次。取代
PrintWriter out = null;
与
PrintWriter out = new PrintWriter(new FileWriter(fileName));
请注意:如果您的NullPointerException
为空,那么对于您当前的代码,最终可能会在行out.close();
处ArrayList
结束。
答案 2 :(得分:0)
首先:你正在编写java,而不是C ++。使用Java结构和技术。
如MadConan所述,您的实施过度。在每个对象类型(费用和收入)上使用toString()(或toBlammy() - blammy不是字符串)来格式化输出。
提示:只要你有一堆if (instanceof blammy)
,就应该考虑多态性。
你的代码应该是这样的:
public void writeListToFile(
final String fileName,
final List<BudgetItem> listBudgetItem)
throws Exception
{
PrintWriter out = null;
try
{
out = new PrintWriter(new FileWriter(fileName));
for(BudgetItem current : listBudgetItem)
{
out.println(current.toBlammy());
}
}
catch (... exceptions)
{
}
finally
{
// close the PrintWriter.
}
}