How to append data to a text file in java?
Hello to all,
I am new to this forum. I am last year Computer science student. As part of my syllabus I am working on project where I use java as front end. In that project I have to append data to a text file. I tried various method, but none of them worked out. Can anyone tell me how to append data to a text file in java?
Thanks in advanced.
Re: How to append data to a text file in java?
Hey it is very easy process to append data to a text file in java. You have to just use java.io and FileWriter class to do this. FileWriter class provides constructor that is used to accept a boolean parameter call append. In the following program you have to set boolean value true to append data to a text file.
Code:
package sample.code.java.io;
import java.io.*;
public class AppendFileEg
{
public static void main(String[] args)
{
File files = new File("sample.txt");
try
{
FileWriter writers = new FileWriter(files, true);
writer.write("usernames=omen;password=12345"
+ System.getProperty("line.separator"));
writers.flush();
writers.close();
} catch (IOException es)
{
es.printStackTrace();
}
}
}
Re: How to append data to a text file in java?
You have to use BufferedWriter class and FileWriter class to append data to a text file. To do this you have to first invoke FileWriter constructor in proper way. I have written small java program to append data to a text file. In following program I have create one class known as appendfileEg class.
Code:
import java.io.*;
public class appendfileEg {
public static void main (String[] args) {
JavaFileAppendFileWriterExample as = new JavaFileAppendFileWriterExample();
as.appendToCheckbook();
}
public void appendToCheckbooks () {
BufferedWriter bws = null;
try {
bws = new BufferedWriter(new FileWriter("sample1.dat", true));
bws.write("406:036987998:Inprise Corporation:236.95");
bws.newLine();
bws.flush();
} catch (IOException ioes) {
ioes.printStackTrace();
} finally {
if (bws != null) try {
bws.close();
} catch (IOException ioe2s) {
}
}
}
}
Re: How to append data to a text file in java?
It is very simple to append data to a text file in java. It is very small program to understand. In the following program I have use two class known as FileWriter and BufferedWriter to append the data to a file. The FileWriter class is usually used for writing character files and the BufferWriter class is commonly used to write text to a character-output stream like string.
Code:
import java.io.*;
class FileWrite
{
public static void main(String args[])
{
try{
FileWriter fstreams = new FileWriter("sampl1.txt",true);
BufferedWriter outs = new BufferedWriter(fstream);
out.write("Hello to this world");
outs.close();
}catch (Exception es){
System.err.println("Errors: " + es.getMessage());
}
}
}
Re: How to append data to a text file in java?
As per my knowledge it is very simple to append data to text file in java. You only have to used only two class FileWriter and BufferedWriter to append data to a text file. It is very simple program. Just use this code in your program to do this. In following program I have crated one object of BufferedWriter and save all data information in that object
Code:
try {
BufferedWriter outs = new BufferedWriter(new FileWriter("samplefilenames", true));
outs.write("aString");
outs.close();
} catch (IOException e) {}