So I have been trying to write a Bukkit plugin for a friend of mine, and for some reason the config generation is not working. The code in question is below, and I will be happy to add any code that people need to help me out with this. When I run the program, the config file that was created ends up blank. The testing file is just fine (I tested that by simply commenting out the line that deletes the file) but once I try to get multiple lines it fails. Can anyone help?
PrintWriter out = new PrintWriter(new FileWriter(config));
out.println("########################################");
out.println("# hPlugin is written by newbiedoodle #");
out.println("########################################");
out.println("# ----- Plugin config file ----- #");
out.println("########################################");
out.println("NOTE: Do not edit the config file besides changing the values - it may result in errors.");
out.println("--------------");
out.println("Strikes before being banned?");
out.println("3");
out.println("Godmode?");
out.println("true");
out.println("First time running the plugin?");
out.println("true");
out.println("Curse protection?");
out.println("true");
out.println("Emergency shelter?");
out.println("true");
out.println("Path building?");
out.println("true");
out.println("Blocked words/phrases (Separate with comma)");
out.println("[censored]");
out.close();
System.out.println("Successfully wrote defaults to config");
The whole thing is enclosed in a try/catch loop just to catch any errors that may pop up. I get a feeling that I am missing something extremely obvious, but I can't find what it is.
config
is aFile
object with the path that it needs to have, so I don't think that it's thatI have the program do almost everything separately so that I can tell the user exactly where an error happened, so the file is created just outside of the
try
/catch
.
Most likely you have run this more than once and you are writing to one file and checking another. I suspect the filenames are the same but the directory, perhaps based on the working directory is not what you think it is.
Close calls flush in this situation as PrintWriter uses BufferedWriter and the other Writer class are not buffered.
You need to flush or close the stream when you're done. Both are very important before exiting. Close will automatically call flush().
You need to call...
... right before you call...
PrintWriter uses buffer in memory to make more efficient use of the disk. You need to call flush() to tell the PrintWriter to write your data.