Search This Blog

Thursday, 30 January 2014

writing to a file in java

/* program to illustrate the use of FileWriter to write to a file */


package javaio;

import java.io.FileWriter;
import java.io.*;

class MainClass
{
    public static void main(String[] args)
    {
        String fName="E://MyFile.txt";
        String content="sanjay kumar singh";
        try
        {
         /* This one is the first method  
          * FileWriter f=new FileWriter(fName,true);
            BufferedWriter bw=new BufferedWriter(f);
            bw.write(content);
            f.write(content);
            f.close();*/
            
            File f=new File(fName);
            FileWriter f2=new FileWriter(f,true);
            f2.write(content);
            System.out.println("Writing in a file is successful ...");
            f2.close();
        }
        catch(Exception e)
        {
            System.err.println("Error is :"+e.getMessage());
        }
    }
}

output

run:
Writing in a file is successful ...
BUILD SUCCESSFUL (total time: 0 seconds)


No comments:

Popular Posts