Java FileInputStream Copy copyFile(File from, File to)

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

Description

copy File

License

Apache License

Declaration

public static void copyFile(File from, File to) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    public static void copyFile(File from, File to) {
        try {//  w w  w  .  ja  v  a 2 s  .c o m
            FileInputStream inputStream = new FileInputStream(from);
            FileOutputStream outputStream = new FileOutputStream(to);
            byte[] b = new byte[1024];
            int n = 0;
            while ((n = inputStream.read(b)) != -1) {
                outputStream.write(b, 0, n);
            }
            inputStream.close();
            outputStream.flush();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copyFile(String from, String to) {
        try {
            FileInputStream inputStream = new FileInputStream(from);
            FileOutputStream outputStream = new FileOutputStream(to);
            byte[] b = new byte[1024];
            int n = 0;
            while ((n = inputStream.read(b)) != -1) {
                outputStream.write(b, 0, n);
            }
            inputStream.close();
            outputStream.flush();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. copyFile(File file, File dir)
  2. copyFile(File file, File newFile, boolean overwrite, boolean setLastModified)
  3. copyFile(File file, OutputStream os)
  4. copyFile(File fileSrc, File fileDest)
  5. copyFile(File fileToCopy, File targetDir)
  6. copyFile(File from, File to)
  7. copyFile(File from, File to)
  8. copyFile(File from, File to)
  9. copyFile(File from, File to)