BufferedWriter是否需要Getter?同类数据无法写入问题求助
Hey there! Let's figure out why your BufferedWriter works fine when writing data from other classes (via getters) but fails to write your current class's internal info—even with no errors. I’ve dealt with this exact confusion before, so here are the top things to check:
1. You’re Not Flushing/Closing the BufferedWriter Properly
BufferedWriter uses an internal buffer to batch writes for efficiency. If you only call write() and never trigger a flush or close the stream, the buffer might hold onto your internal class data instead of writing it to the file. This is especially common if your program exits right after writing without letting the JVM clean up.
Fix it with try-with-resources (the safest approach):
try (BufferedWriter bw = new BufferedWriter(new FileWriter("your-file.txt"))) { // Write other class data (works fine) bw.write("Other class data: " + externalClass.getSomeInfo()); bw.newLine(); // Add a line break for readability // Write internal class data bw.write("Internal class data: " + this.getInternalInfo()); // Optional: Manually flush to ensure data is written immediately bw.flush(); } catch (IOException e) { e.printStackTrace(); // This will catch any hidden errors you might have missed }
The try-with-resources block automatically closes the stream when done, which forces all buffered data to be written to the file.
2. Your Internal Data’s Getter is Returning Empty/Null Content
Double-check the getter for your internal class info—if it’s returning an empty string, null, or an uninitialized value, the writer will write nothing (or "null") which looks like it failed.
Test this quickly:
Add a print statement right before writing to confirm what you’re trying to write:
String internalData = getInternalInfo(); System.out.println("Attempting to write internal data: " + internalData); // Verify this isn't empty/null bw.write(internalData);
If the print shows nothing or "null", fix your getter to return the actual internal variable value.
3. Your Internal Data is Being Written but You Can’t See It
Sometimes the data is written, but it’s hidden because:
- You forgot to add a line break (
bw.newLine()or\n) after the previous write, so the internal data is stuck at the end of the other class’s text. - You’re looking at the wrong file! If you’re using a relative path, the file might be in your project’s root instead of the directory you expect. Check your program’s working directory to confirm the file location.
4. Hidden Exceptions (Yes, Even if You Think There Are None)
If you’re not catching exceptions properly, an error could be occurring silently. Make sure your write code is wrapped in a try-catch block that prints the stack trace—this will reveal any issues like permission errors or invalid characters in your internal data that you didn’t notice before.
Start with these checks, and you’ll almost certainly find why your internal class data isn’t showing up in the file.
内容的提问来源于stack exchange,提问作者Anjula Serasinghe

