Friday 15 March 2013

File to Byte Array and vice verse



FILE TO BYTE & BYTE TO FILE

Converting a file to byte array and retrieving it from that, mostly used in programming world especially in case of storage purposes. Here we show how simply we can achieve it with java File Stream operators - "FileInputStream, FileOutputStream".
     
     Source Code:


public static void main(String[] args) {
        System.out.println("START");
        byte[] byteArray = null;
        FileInputStream fin = null;
        FileOutputStream fout = null; 
        try {
            fout = new FileOutputStream("D:\\WaterTalksOut.pdf");
            fin = new FileInputStream("D:\\WaterTalksIn.pdf");
            byteArray= new byte[fin.available()];
            fin.read(byteArray);
            fout.write(byteArray);
        System.out.println("END");    
        } catch (Exception ex) {
            Logger.getLogger(FileOperations.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("END WITH ERROR");
        }
}

No comments:

Post a Comment