I am working with Java.
I have created one text file. In that text file, I want one value to be stored in the text file from class1. Then I want one more value to be stored in the textfile from class2 on the 2nd line. Then I want one more value to be stored in line 3 of the text file from class 3 , etc. The text file, when retrieved, should look like this:
77
65
34
The problem I encounter is that, the text file content gets erased when I move on to the next class. For example, the text file would only show the last data value. And erase the previous ones (in the example above, only 34 would appear that too on the first line, when it should be on the 3rd line).
Some code from class1:
BufferedWriter outputFile0=null;
try {
int x = value_MAP();
FileWriter fwriter0 =new FileWriter("MAP_allData.txt");
outputFile0 = new BufferedWriter(fwriter0);
outputFile0.write(""+x);
outputFile0.newLine();
} catch( Exception y ) {y.printStackTrace();}
Same code again appears in class2, class3, etc:
BufferedWriter outputFile0=null;
try {
int x = value_MAP();
FileWriter fwriter0 =new FileWriter("MAP_allData.txt");
outputFile0 = new BufferedWriter(fwriter0);
outputFile0.write(""+x);
outputFile0.newLine();
} catch( Exception y ) {y.printStackTrace();}
So is there a way to tell Java that dont worry about erasing the data, just add data one line after another when traveling from class 1 to class x.
To appnend and not to erase existing data in a file, you should be using the FileWriter constructor like this..
The second ‘boolean’ argument ‘true’ in the constructor will tell the file writer to append.
javadoc