Tuesday, 6 December 2016

read and write file in java

writter:

package file.writer.and.reader;
import java.io.*;

/**
 *
 * @author Vishnuyardini
 */
public class writter {
    public static void main(String ... arg)
    {
        try{
   FileWriter fw=new FileWriter("abc.txt");
   fw.write("hai parwez.. this is my first task.. i did this");
   fw.close();
  }
        catch(Exception e){System.out.println(e);}
  System.out.println("success content was success fully write");
 }
    }


reader:

package file.writer.and.reader;
import java.io.*;

/**
 *
 * @author Vishnuyardini
 */
public class reader {
    public static void main(String... arg)throws Exception{  
        try (FileReader fr = new FileReader("abc.txt")) {
            int i;
            while((i=fr.read())!=-1)
                System.out.print((char)i);
        }  
    
}
}

No comments:

Post a Comment