How to copy binary file using FileInputStream() and FileOutputStream() in Java

1 Answer

0 votes
package javaapplication1;

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

public class JavaApplication1 {

    public static void main(String[] args) {

        String sSource = "d://Img_1973.jpg";
        String sDestination = "d://Img_1973_Backup.jpg";

        try {

            FileOutputStream fos;
            try (FileInputStream fis = new FileInputStream(sSource)) {
                fos = new FileOutputStream(sDestination);
                byte[] b = new byte[1024];
                int totalBytes = 0;
                while ((totalBytes = fis.read(b)) != -1) {
                    fos.write(b, 0, totalBytes);
                }
            }

        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }
}

// check your directory for destination file

/*
      
run:

  
 */

 



answered Nov 13, 2016 by avibootz

Related questions

1 answer 175 views
1 answer 165 views
1 answer 193 views
1 answer 259 views
1 answer 151 views
1 answer 185 views
...