When we develop an application, sometimes we need to read a file from disk. Not only character based files, but also byte/binary based files. To read/write character based files we use Writer and Reader class hierarchy, to read byte/binary based files we use InputSteam and OutputStream class hierarchy.
This article will show how to read binary files such as EXE, PDF, and make a copy of that file on different location on disk. We will open the file, read byte by byte, and write all the bytes we get to make a copy of that file.
I use three different way to get the bytes from the file as far as i know. I dont know which one is the best based on the performance or algorithm :p.
(Way 1)
File file = new File(PATH_TO_FILES); fis = new FileInputStream(file); dis = new DataInputStream(new BufferedInputStream(fis)); fos = new FileOutputStream(DESTINATION_PATH_AND_FILE_NAME); dos = new DataOutputStream(new BufferedOutputStream(fos)); byte b; try{ while(true){ b = (byte) dis.readByte(); dos.write(b); } } catch(EOFException e){ System.err.println("Finish read end of file..."); } finally{ dos.close(); }(Way 2)
File file = new File(PATH_TO_FILES); fis = new FileInputStream(file); dis = new DataInputStream(new BufferedInputStream(fis)); fos = new FileOutputStream(DESTINATION_PATH_AND_FILE_NAME); dos = new DataOutputStream(new BufferedOutputStream(fos)); byte[] b = new byte[(int) file.length()]; for(int i=0; i<b.length-1; i++) { b[i] = dis.readByte(); } dos.write(b); dos.close(); [/sourcecode]</pre> <strong>(Way 3)</strong> <pre> File file = new File(PATH_TO_FILES); fis = new FileInputStream(file); dis = new DataInputStream(new BufferedInputStream(fis)); fos = new FileOutputStream(DESTINATION_PATH_AND_FILE_NAME); dos = new DataOutputStream(new BufferedOutputStream(fos)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = new byte[200]; while(true){ int length = dis.read(b); System.out.println("read : " + length); if (length < 0) { break; } if(length > 0){ baos.write(b, 0, length); } } dos.write(baos.toByteArray()); dos.close();(Full source code)
/** * @author : gardiary * @Waktu : Jul 28, 2008, 4:33:14 PM * * StreamSample.java */ package test.filehandling; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class StreamSample { private File file; private FileInputStream fis; private FileOutputStream fos; private DataInputStream dis; private DataOutputStream dos; public StreamSample() { file = new File("input/ajax-tutorial.pdf"); } public static void main(String args[]) { StreamSample app = new StreamSample(); app.copyFile1(); System.out.println("Done copy file 1..."); app.copyFile2(); System.out.println("Done copy file 2..."); app.copyFile3(); System.out.println("Done copy file 3..."); try{ app.dis.close(); app.fis.close(); } catch(IOException ioe){ ioe.printStackTrace(); } } // copy file first way public void copyFile1() { openFileInput(); try{ fos = new FileOutputStream("output/copy-of-ajax-tutorial-1.pdf"); dos = new DataOutputStream(new BufferedOutputStream(fos)); byte b; try{ while(true){ b = dis.readByte(); dos.write(b); } } catch(EOFException e){ System.err.println("Finish read end of file..."); } } catch(IOException e){ System.err.println("IOException...1"); } finally{ closeOutputStream(dos); closeOutputStream(fos); } } // copy file second way public void copyFile2() { openFileInput(); try{ fos = new FileOutputStream("output/copy-of-ajax-tutorial-2.pdf"); dos = new DataOutputStream(new BufferedOutputStream(fos)); byte[] b = new byte[(int) file.length()]; for(int i=0; i<b.length-1; i++) { b[i] = dis.readByte(); } dos.write(b); } catch(IOException e){ System.err.println("IOException...2"); e.printStackTrace(); } finally{ closeOutputStream(dos); closeOutputStream(fos); } } // copy file third way public void copyFile3() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = new byte[200]; openFileInput(); try{ fos = new FileOutputStream("output/copy-of-ajax-tutorial-3.pdf"); dos = new DataOutputStream(new BufferedOutputStream(fos)); while(true){ int length = dis.read(b); System.out.println("read : " + length); if (length < 0) { break; } if(length > 0){ baos.write(b, 0, length); } } dos.write(baos.toByteArray()); } catch(IOException e){ System.err.println("IOException...3"); } finally{ closeOutputStream(dos); closeOutputStream(fos); } } // open file for input public void openFileInput() { try{ fis = new FileInputStream(file); dis = new DataInputStream(new BufferedInputStream(fis)); } catch(FileNotFoundException e){ System.err.println("File Not Found...!"); } } // close output stream public void closeOutputStream(OutputStream os) { try{ os.close(); } catch(IOException ioe){ System.err.println("Exception closing output stream"); } } }