Buffering used to Read Images or COPY image


/*
           Author:ZT

           Date : Aug 24,2017




*/
package a.b.c
 
import java.io.*;
 
public class Buffering {
 
    public static void main(String[] args) {
 
        //If you are using an older version and not using try with resources
        //than don't forget to close the streams in the finally block.
        try (
        // Byte Streams
        // Either use a fully qualified name of the image or just use the name
        //and ensure that the image is in classpath.
        InputStream inputStream = new FileInputStream("d:\\java2se16\\imag1.jpg");
                OutputStream outputStream = new FileOutputStream("imag2.jpg");
 
                // Buffered Streams
                // Enveloping Byte Streams into Buffered Streams
                BufferedInputStream bufferIn = new BufferedInputStream(inputStream);
                BufferedOutputStream bufferOut = new BufferedOutputStream(outputStream)) {
 
            // This array stores the data read in the form of Bytes.
            byte[] buffer = new byte[1024];
 
            // Looping till we reach the end of file i.e value returned is -1
            while (bufferIn.read(buffer) != -1) {
 
                // writing to output buffer
                bufferOut.write(buffer);
            }
            System.out.println("File copied successfully");
        } catch (FileNotFoundException e) {
            System.out.println("Input file is not found");
        } catch (IOException e) {
            System.out.println("Error in Reading and writing operations");
        }
    }
}