writing to files in java

J

js_dev

Writing to files

How do I write to a file in Java?

(a)
Using FileWriter:
import java.io.FileWriter; //import needed

String contents = .....; /* String to be written */
try{ /* try-catch will be required for compiling successfully */
FileWriter fw = new FileWriter(fullpath);
fw.write(contents);
fw.flush();
fw.close();
}catch(Exception e){System.out.println("Exception, text:" + e);}

(b)
import java.io.FileOutputStream; //import needed

String s = ..... ; /* String to be written */
try{
FileOutputStream fos = new FileOutputStream(fullpath);
for (int i=0; i<s.length();++i){
fos.write((byte)s.charAt(i));
}
fos.flush();
fos.close();
}catch(Exception e){System("Exception :"+ e);}

(c)
Appending to a file:

import java.io.FileWriter; //import needed

String fullpath = ..... ; /* fullpath to the file to be appended to */
String contents= .....; /* String to be written */
try{
FileWriter fw = new FileWriter(fullpath,true);
//append the string character by character
//because no String append method is given

for (int i=0; i<contents.length(); ++i){
fw.append(contents.charAt(i));
}
fw.flush();
fw.close();
}catch(Exception e){appendlog("Exception :" + e);}
 
S

Sebastian Scheid

Writing to files

How do I write to a file in Java?

(a)
Using FileWriter:

FileWriter is appropriate because that is made for characterstreams which
you have (you are writing a String).
FileWriter fw = new FileWriter(fullpath);
fw.write(contents);
fw.flush();
fw.close();

Since you are writing a single String and don't call write() repeatedly,
writing directly (unbuffered) to a FileWriter may result in the best
performance.
If repeated calls to write are expected, wrap a BufferedWriter around the
FileWriter:
Writer writer = new BufferedWriter(new FileWriter(...);
....
(b)
import java.io.FileOutputStream; //import needed

String s = ..... ; /* String to be written */
try{
FileOutputStream fos = new FileOutputStream(fullpath);
for (int i=0; i<s.length();++i){
fos.write((byte)s.charAt(i));

An OutputStream writes plain bytes and does no character encoding for you.
Don't use it for Strings.
If you use one, it's essential to wrap a BufferedOutputStream around if you
write to it in a loop like here!

(c)
Appending to a file:

import java.io.FileWriter; //import needed

String fullpath = ..... ; /* fullpath to the file to be appended to */
String contents= .....; /* String to be written */
try{
FileWriter fw = new FileWriter(fullpath,true);

The append flag in the constructor is the only change necessary.
//append the string character by character
//because no String append method is given

for (int i=0; i<contents.length(); ++i){
fw.append(contents.charAt(i));

Read the api doc for append. There is no difference to write.
Again use a BufferedWriter when calling write() often!


So, use a) if you write a single string and use a BufferedWriter to improve
performance when writing many strings.

Regards
Sebastian
 
Joined
Jun 12, 2009
Messages
2
Reaction score
0
Hi,
The following code creates a log file and appends data to it...
public void createlogfile(String message){
BufferedWriter out1=null;
try {
File file = new File(this.readProperty("OUTPUT_FOLDER")+"//"+this.getCurrentDate("ddMMyyyy", Calendar.getInstance().getTime())+".log");

out1 = new BufferedWriter(new FileWriter(file,true));
out1.write(message);
out1.newLine();
out1.flush();

} catch (IOException e) {
e.printStackTrace();

} finally { // always close the file
if (out1 != null) try {
out1.close();
} catch (IOException ioe) {
// just ignore it
}
}

}
At present though everytime the function is called by a diff function... the log is overwritten and in the end only the lines added by the last function remains....
could anyone help me with this???
Thanks.... :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top