Java FileInputStream Copy copyFile(File src, File dest)

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

Description

copy File

License

Apache License

Declaration

public static void copyFile(File src, File dest) throws IOException 

Method Source Code


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

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

public class Main {
    public static void copyFile(File src, File dest) throws IOException {
        byte[] bytes = readFile(src);
        createFile(bytes, dest);//from   w  w w .ja v a 2 s. c  o  m
    }

    public static byte[] readFile(File file) throws IOException {
        int length = (int) file.length();
        FileInputStream fis = new FileInputStream(file);
        byte[] result = new byte[length];
        int pos = 0;
        while (pos < length) {
            pos += fis.read(result, pos, length - pos);
        }
        fis.close();
        return result;
    }

    public static void createFile(byte[] src, File outFile) throws IOException {
        FileOutputStream fout = new FileOutputStream(outFile);
        fout.write(src);
        fout.close();
    }

    public static void createFile(String src, File outFile) throws IOException {
        createParentDirs(outFile);
        createFile(src.getBytes(), outFile);
    }

    public static void createParentDirs(File file) {
        String prefix = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(File.separator));
        new File(prefix).mkdirs();
    }
}

Related

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