Files written with FileWriter are either 32 KB, 24 KB, 16 KB, ... big or empty
I read a file into a string, change the first line and then write this string into a new file. I do this through the fo开发者_开发技巧llowing code (a little bit shortened):
while(jspIterator.hasNext()){
String line = (String) jspIterator.next();
if (i == 0) {
if (line.startsWith("bla bla") && line.endsWith("yada")) {
line = line.replaceFirst("this", "that");
}
}
jspAsString += line;
i++;
}
FileWriter newJspWriter = new FileWriter(newJspFile);
newJspWriter.write(jspAsString);
Now the files written this way are either 32, 24, 16, 8 KByte big or completely empty. When debugging I see that the String is assembled correctly. When I print the variable jspAsString to the console it also appears correct.
Do you know why FileWriter behaves this way?
More than likely you did not close()
the FileWriter
.
Related questions
- Closing Streams in Java
- Closing a Java FileInputStream
- Does close ever throw an IOException?
- Does closing a BufferedOutputStream also close the underlying OutputStream?
精彩评论