Copy JPG File - Java File Path IO

Java examples for File Path IO:FileInputStream

Description

Copy JPG File

Demo Code

import java.io.*;

public class CopyJPGFile {
    public static void main(String[] args) {
        try (FileInputStream source = new FileInputStream(new File("files/picture.jpg"));
             FileOutputStream destination = new FileOutputStream(new File("files/my-copied-picture.jpg"))){

            byte[] buffer = new byte[4096];
            while (true){
                int readBytes = source.read(buffer,0,buffer.length);
                if(readBytes <= 0){
                    break;
                } else {
                    destination.write(buffer,0,readBytes);
                }/*from   w w w .  ja  v  a 2 s .c  om*/
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials