Java FileInputStream Copy copyFile(File source, File dest)

Here you can find the source of copyFile(File source, File dest)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File source, File dest) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static void copyFile(File source, File dest) {

        if (!dest.exists()) {

            try {
                dest.createNewFile();/*from   ww  w. jav a2  s .c om*/
            } catch (IOException e) {

                e.printStackTrace();
            }

        }

        InputStream in = null;

        OutputStream out = null;

        try {

            in = new FileInputStream(source);

            out = new FileOutputStream(dest);

            byte[] buf = new byte[1024];

            int len;

            while ((len = in.read(buf)) > 0) {

                out.write(buf, 0, len);

            }

        } catch (FileNotFoundException e) {

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

            e.printStackTrace();
        }

        finally {

            try {
                in.close();
                out.close();
            } catch (IOException e) {

                e.printStackTrace();
            }

        }

    }
}

Related

  1. copyFile(File source, File dest)
  2. copyFile(File source, File dest)
  3. copyFile(File source, File dest)
  4. copyFile(File source, File dest)
  5. copyFile(File source, File dest)
  6. copyFile(File source, File destination)
  7. copyFile(File source, File destination)
  8. copyFile(File source, File destination)
  9. copyFile(File source, File destination)