|
| ||||||||||
| Tags: class, do while, java, loop statement, object, program, project |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| How to append data to a text file in java?
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. Last edited by KALANI84 : 05-02-2010 at 03:20 PM. |
|
#2
| ||||
| ||||
| 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();
}
}
} |
|
#3
| ||||
| ||||
| 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) {
}
}
}
} |
|
#4
| ||||
| ||||
| 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());
}
}
} |
|
#5
| ||||
| ||||
| 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) {} |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "How to append data to a text file in java?" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to rename .Bat-append fixed text in c# | Omanand | Software Development | 5 | 05-07-2011 07:24 PM |
| Open text file and edit it in java | Gillian Anderson | Software Development | 4 | 29-03-2010 01:56 PM |
| Append text to end of file | New ID | Software Development | 5 | 18-01-2010 08:35 AM |
| Does java parse large text file | Zeverto | Software Development | 3 | 30-07-2009 01:26 PM |
| How to append an existing file in Java? | NinjaZxR | Software Development | 3 | 28-07-2009 05:55 PM |