Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Append To File - Java Tutorial 
 

In the section of Java Tutorial you will learn how to write java program to append a file.

 

Append To File - Java Tutorial

                         

introduction

In the section, you will learn how the data is appended to an existing file. We will use the class FileWriter and BufferedWriter to append the data to a file.

 FileWriter
The FileWriter is a class used for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. This constructor simply overwrite the contents in the file by the specified string but if you put the boolean value as true with the file name (argument of the constructor) then the constructor append the specified data to the file i.e. the pre-exist data in a file is not overwritten and the new data is appended after the pre-exist data.

BufferedWriter

The BufferWriter class is used to write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

Here is the code of java program to write text to a file:

import java.io.*;
class FileWrite 
{
   public static void main(String args[])
  {
      try{
    // Create file 
    FileWriter fstream = new FileWriter("out.txt",true);
        BufferedWriter out = new BufferedWriter(fstream);
    out.write("Hello Java");
    //Close the output stream
    out.close();
    }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
  }
}

Download the code

                         

» View all related tutorials
Related Tags: c orm form time script object io objects help method sed system ip collection this opera create show for work

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

7 comments so far (
post your own) View All Comments Latest 10 Comments:

"new" a FileWriter with "true" argument will open a file if it exists.

Posted by vcgu on Thursday, 07.9.09 @ 19:33pm | #89201

The code only write to a file but not append to a file. Useless and misleading tutorial and a wasting of time.

Posted by Sara Smith on Tuesday, 04.7.09 @ 06:26am | #86581

Are you guys stupid? this code works great. Thanks for this realy simple tutorial. I wish more tutorials would be as to the point as this one!

Posted by Jules on Wednesday, 12.17.08 @ 07:53am | #82874

That is NOT APPEND, this does not writes in the end of the file. IT OVERWRITES!!!!!

you suck

Posted by miner on Saturday, 12.13.08 @ 12:22pm | #82723

hello
i used the above code in 1 of my programs but its not writing data in to the out.temp file.
what i did is that i parsed the xml doc by DOM parser and trying to store the value in the temp file after paring,p[arsing is doing perfect but not writing the data into the temp file.
can you put light on this .
thanks

Posted by ajay on Monday, 09.29.08 @ 12:18pm | #80765

This is one of the best tutorials I have ever found on the net. Thanks for the good work.

Posted by Christopher on Tuesday, 09.23.08 @ 14:47pm | #80628

you gerat man , i ma thanks full to you

Posted by murali on Monday, 11.12.07 @ 09:58am | #36937

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.