Friday, August 15, 2014

Posted by Anmol Sharma
In the previous article, we discussed about two basic file handling functions in Java including searching for an existing created file in the system and creating a file using java’s inbuilt function.

In this article we will cover up the other two file handling operations which includes reading from a file and writing to a file using java’s own methods. In this program we create a file by specifying a file name, open it at the same time and add records to it as per custom format. After writing to the file, it is good practice to close it.

import java.io.*;
import java.lang.*;
import java.util.*;

public class FileHandlingTrial {

       private Formatter file;
      
              public void openFile (){ //creating & opening a file
                    
                     try{
                           file= new Formatter("New Text Document.txt"); //will be created in default workspace folder else specify a path
                     }
                    
                     catch(Exception e){
                           System.out.println("Illegal file operation");
                                 
                           }
                     }
                    
                     public void addRecords(){// writes to the created file
                           file.format("%s %s %s", "99", "MCA", "IP University");
                    
                     }
                    
                     public void closeFile(){ //good practice to close the file after use
                           file.close();
                     }
      
}


class FileHandlingTrial1 {
      
       public static void main(String[] args){
             
         FileHandlingTrial asp = new FileHandlingTrial();
        
         asp.openFile();
         asp.addRecords();
         asp.closeFile();
             
       }
}


In this last program of file handling we will read the content added by the user.
Here also we create three methods in which first opens the file, second reads the content of the file and third one finally closes the file after reading the contents.


import java.io.*;
import java.util.*;



public class FileHandlingTrial {

private Scanner file;

public void openFile(){ // to open the file
      
       try{
              file = new Scanner(new File("new.txt"));
             
             
       }
      
       catch(Exception e){
             
              System.out.println("File not found");
       }
}

public void readFile(){ // to read data from the file
      
       while(file.hasNext()){
              String a=file.next();
              String b=file.next();
              String c=file.next();
              String d=file.next();
             
              System.out.printf("%s %s %s %s\n", a, b, c, d);
             
}

}

public void closeFile(){
file.close();
}
}


class FileHandlingTrial1 {
      
       public static void main(String[] args){
             
         FileHandlingTrial asp = new FileHandlingTrial();
        
         asp.openFile();
         asp.readFile();
         asp.closeFile();
             
       }
}

Screenshot // click to open a higher resolution version





0 comments:

Post a Comment