This is my code for reading file and writing file ; i want to write string content to the end of the text file. My aim is to have command on cursor movement/controlling in a text file.
please help me out thanks
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Filing {
public static void main(String[] args) throws IOException
{
String content = "This is the content to write into file";
BufferedReader br =
new BufferedReader(new FileReader("C:/Users/Ashad/Desktop/text.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
FileWriter fw = new FileWriter("C:/Users/Ashad/Desktop/text.txt");
BufferedWriter bw = new BufferedWriter(fw);
while (line != null)
{
sb.append(line);
sb.append("\n");
line = br.readLine();
}
//** bw.write(content) ; **//
String everything = sb.toString();
System.out.append(everything);
}
finally
{ br.close();}
I couldnot write String content after the text already present on the file.
i want to write string content to the end of the text file
Then just use the overload of the FileWriter
constructor that takes an append
flag:
FileWriter fw = new FileWriter("C:/Users/Ashad/Desktop/text.txt", true);
Personally though, I'd prefer to use FileOutputStream
and an OutputStreamWriter
- that way you can specify the encoding.