Sunday, March 16, 2014

Simple File Handling in Java | Example | Source code

Written by Pranshu Bajpai |  | LinkedIn

Here's a simple code that illustrates File Handling in Java:

Source Code



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DICAss;
import java.io.*;

/**
 *
 * @author Pranshu
 */
public class DICAss0_FileHandling {
    
    
    
    public static void main(String[] a){
        int Flag = 0;
        String FileName;
        String FileBody;
        int opt;
        
                
        BufferedReader ObjTake = new BufferedReader(
                new InputStreamReader(System.in));
        
        System.out.println("Choose an Option: \n 1. File Writing \n 2. File Reading");
        
        try{
            System.out.println("What you want to do?? enter option : ");
            opt = Integer.parseInt(ObjTake.readLine());
            
            switch (opt){
                case 1:
                    System.out.print("Enter Name of the File (with extension): ");
                    FileName = ObjTake.readLine();
                    
                    System.out.print("Enter Body of the File : ");
                    FileBody = ObjTake.readLine();
                    
                    Flag = FileWriteFunc(FileName, FileBody);
                    
                    if(Flag == 0){
                        System.err.println("Unable to write!!!");
                    }
                break;
                    
                case 2:
                    System.out.print("Enter Name of the File (with extension): ");
                    FileName = ObjTake.readLine();
                    
                    String FText;
                     FText = FileReadFunc(FileName);
                     System.out.println("Text in file : " + FText);
                    
                    if(FText == null){
                        System.err.println("Unable to read!!!");
                    }
                break;
                    
                default:
                    System.err.println("Invalid Option!!!");
            }
        }
        catch(Exception err){
            System.err.println("Error : " + err);
        }
    }
    
    public static int FileWriteFunc(String FName, String FBody){
        FileOutputStream ObjOut;
        PrintStream ObjPrint;
        
        try{
            ObjOut = new FileOutputStream(FName);
            
            ObjPrint = new PrintStream(ObjOut);
            
            ObjPrint.print(FBody);
            ObjPrint.close();
            
            //System.out.println("Writing Complete!!!");
            
            return 1;
        }
        catch(Exception err){
            System.err.println("Error: " + err);
            return 0;
        }
    }
    
    public static String FileReadFunc(String FName){          
        try{
            FileInputStream FStream = new FileInputStream(FName);
            
            DataInputStream ObjIn = new DataInputStream(FStream);         
         
            String str = "";
            while (ObjIn.available() != 0){
                //System.out.println(ObjIn.readLine());
                str = str + ObjIn.readLine();
            }
            
            ObjIn.close();
            
            //System.out.println("reading Complete!!!");
            return str;
        }
        
        catch(Exception err){
            System.err.println("Error : " + err);
            return null;
        }        
    }
}

No comments:

Post a Comment