Results 1 to 5 of 5

Thread: How to append data to a text file in java?

  1. #1
    Join Date
    Aug 2009
    Posts
    56

    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.
    Last edited by KALANI84; 05-02-2010 at 04:20 PM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    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. #3
    Join Date
    Apr 2008
    Posts
    2,005

    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. #4
    Join Date
    May 2008
    Posts
    2,297

    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. #5
    Join Date
    Nov 2005
    Posts
    1,323

    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) {}

Similar Threads

  1. How to rename .Bat-append fixed text in c#
    By Omanand in forum Software Development
    Replies: 5
    Last Post: 05-07-2011, 07:24 PM
  2. Open text file and edit it in java
    By Gillian Anderson in forum Software Development
    Replies: 4
    Last Post: 29-03-2010, 01:56 PM
  3. Append text to end of file
    By New ID in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 09:35 AM
  4. Does java parse large text file
    By Zeverto in forum Software Development
    Replies: 3
    Last Post: 30-07-2009, 01:26 PM
  5. How to append an existing file in Java?
    By NinjaZxR in forum Software Development
    Replies: 3
    Last Post: 28-07-2009, 05:55 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,645,511.27182 seconds with 17 queries