I am try to write a java code that will create a .txt file to any directory. And, the code will write some strings to the file & save the text to that file. I have wrote a code that can write to a file, but it can't save the text to my file. Each time I run my code and the new text override the existing text.
Here is my code:
package com.mahbub.file_object;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class WriteFile {
private static String content = "I am a text message!!";
private static BufferedWriter bw;
public static void main(String[] args) {
File file = new File("D:/test.txt");
try {
FileWriter fw = new FileWriter(file);
bw = new BufferedWriter(fw);
bw.write("I am 2nd line");
bw.newLine();
bw.write(content);
bw.close();
System.out.println("done!!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
I want every text line will add into the file one after another without overriding. Can anyone help me? Thanks in advanced!!