AURRR :
I am trying to write into a single text file through different classes. I opened the text file from the main class:
public static void main( String[] args ) throws Exception {
BufferedWriter writer = new BufferedWriter(new FileWriter("src/t1.txt"));
Parser.getParseTree().print(); // where I called the method
writer.close();
}
This is a print() method from another class:
public static void print() throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter("src/t1.txt"));
writer.write("afcad fad fad");
}
The BufferedWriter writer = new BufferedWriter(new FileWriter("src/t1.txt"));
in print() method does not work and nothing prints into the t1.txt file.
GhostCat salutes Monica C. :
Don't have multiple places (methods) that each write to the file on their own.
Simply use one central place that opens the file for writing (probably using try-with-resource) and have any other code call a method at that central place. In other words, all your code should just invoke the same method!