Java-Reading and Writing Raw Binary Files

Program

Program Source

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

class Javaapp {
    
    public static void main(String[] args) {
        
        FileInputStream fi = null;
        FileOutputStream fo = null;
        
        try{
            fi = new FileInputStream("Honda-Japan.jpg");
            fo = new FileOutputStream("Honda-Japan2.jpg");
            int getSize = fi.available();
            System.out.println("File Size : "+getSize);
            
            int i = 0;
            int getByte;
            while(i<getSize)
            {
                getByte = fi.read();
                fo.write(getByte);
                i++;
            }
        }catch(IOException io)
        {
            System.out.println("Exception : "+io);
            
        }finally{
            
            try{
                fo.close();
                fi.close();
                
            }catch(IOException io)
            {
                System.out.println("Exception : "+io);
            }
        }
    }
}

Leave a Comment