Reputation: 3
I'm working on a project for a game I play, and I can't seem to get it to write to a file correctly. I had it working at one point, but changed several things around, and moved the write method into a different class. Somewhere in the process, I must have broken something.
public static void write(item a) {
//Variable declaration. outp is private instance String array
outp[0] = "<" + a.getID() + ">\n";
outp[1] = "<name>" + a.getName() + "</name>";
outp[2] = "<description>" + a.getDesc() + "</description>\n";
outp[3] = "<type>" + a.getType() + "</type>\n";
outp[4] = a.getOtherCode() + "\n";
outp[5] = "</" + a.getID() + ">\n";
try{
//Create/Append data to items.xml located in variable folder.
FileWriter writeItem = new FileWriter(modTest.modName + File.separator +"items.xml", true);
BufferedWriter out = new BufferedWriter(writeItem);
//Loop through array and write everything
for(int i = 0; i < outp.length; i++) {
System.out.println("outp[" + i + "] = " + outp[i]);
System.out.println("Writing line " + i + " of item "+ a.getID());
out.write(outp[i]);
}
}catch (Exception e) { System.err.println("Erro: " + e.getMessage());
}
}
//The File.seperator and ,true) I got from other questions here. I thought those might be the issue, but after commenting them out and just writing items.xml straight to my projects folder, it's still empty. I tried to add some output for debugging purposes, and I got exactly what I expected. All variable outputs match what they should.
The file is created in the correct folder, but nothing is being written to it.
Any ideas on what I did wrong?
Upvotes: 0
Views: 425
Reputation: 16545
You need to close the file descriptor so the contents are flushed to disk.
Add:
out.close();
so that your method becomes:
public static void write(item a) {
//Variable declaration. outp is private instance String array
outp[0] = "<" + a.getID() + ">\n";
outp[1] = "<name>" + a.getName() + "</name>";
outp[2] = "<description>" + a.getDesc() + "</description>\n";
outp[3] = "<type>" + a.getType() + "</type>\n";
outp[4] = a.getOtherCode() + "\n";
outp[5] = "</" + a.getID() + ">\n";
try {
//Create/Append data to items.xml located in variable folder.
FileWriter writeItem = new FileWriter(modTest.modName + File.separator +"items.xml", true);
BufferedWriter out = new BufferedWriter(writeItem);
//Loop through array and write everything
for(int i = 0; i < outp.length; i++) {
System.out.println("outp[" + i + "] = " + outp[i]);
System.out.println("Writing line " + i + " of item "+ a.getID());
out.write(outp[i]);
}
out.close();
}
catch (Exception e) { System.err.println("Erro: " + e.getMessage()); }
}
Without the call to close()
, you're leaking file descriptors and, after a long enough time, you'd be unable to open any more files for writing).
Also note that you're appending to the file each time you're writing to it (rather than truncating it and starting from scratch each time). Since you're writing XML to it, you're unlikely to end up with only one root element.
Upvotes: 1