Read Writer button wont add to file.
my writer reader wont save data and add data to the file. When i run it all it does is overwrites the data. code is :
Code java:
String add = nameField.getText();
String newLine = ("\n");
File file = new File("C:NewTest.dat");
try {
// check whether the file is existed or not
if (file.exists()) {
// create a new file if the file is not existed
file.createNewFile();
}
// new a writer and point the writer to the file
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
// writer the content to the file
writer.write(newLine);
writer.write(add);
// always remember to close the writer
writer.close();
writer = null;
} catch (IOException ab) {
ab.printStackTrace();
}
try {
// new a reader and point the reader to the file
BufferedReader reader = new BufferedReader(new FileReader(file));
nameArea.append(add + newLine);
reader.close();
reader = null;
} catch (FileNotFoundException ab) {
ab.printStackTrace();
} catch (IOException ab) {
ab.printStackTrace();
}
Re: Read Writer button wont add to file.
Quote:
add data to the file
There is an arg in a constructor that will APPEND data to an existing output file instead of creating a new file. Read the API doc to see how to code it.