This method should write random chars, but it doesn’t write anything at all. I’m probably doing something stupidly wrong here, but for the life of me I can’t find it.
public void writeRandomChunk(String fileName) {
try {
File saveFile = new File(folderName + '/' + fileName);
PrintWriter writer = new PrintWriter(
new BufferedWriter(
new FileWriter(saveFile)));
Random r = new Random(System.currentTimeMillis());
for (int i = 0; i < chunkSize; i++) {
for (int j = 0; j < chunkSize; j++) {
writer.print((char)(r.nextInt(26) + 'a'));
}
writer.println();
}
} catch (Exception e) {
System.out.println("Error in WorldFile writeRandomFile:\n"
+ e.getLocalizedMessage());
}
}
As with any stream (and this applies to most any language), you need to close it when you are done.
Streams are optimized to be fast, and as a consequence, not all of the data you write to them instantly appears in the file. When you
close()orflush()a stream, the data is written to the file (or whatever other storage mechanism you are using).In your case, try the following, instead.