复制文件的工具类,采用了输入流输出流的管道来进行复制
package copyfileutil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
public class CopyFile {
public static void copy(String source,String target) throws IOException{
FileInputStream fis=new FileInputStream(source);
FileOutputStream fos=new FileOutputStream(target);
FileChannel cfis=fis.getChannel();
FileChannel cfos=fos.getChannel();
cfis.transferTo(0, fis.available(), cfos);
// cfos.transferFrom(cfis, 0, fis.available());
}
public static void copy(FileInputStream fis,String target) throws IOException{
FileOutputStream fos=new FileOutputStream(target);
FileChannel cfis=fis.getChannel();
FileChannel cfos=fos.getChannel();
cfis.transferTo(0, fis.available(), cfos);
}
public static void copy(String source,FileOutputStream fos) throws IOException{
FileInputStream fis=new FileInputStream(source);
FileChannel cfis=fis.getChannel();
FileChannel cfos=fos.getChannel();
cfis.transferTo(0, fis.available(), cfos);
}
public static void copy(File source,File target) throws IOException{
FileInputStream fis=new FileInputStream(source);
FileOutputStream fos=new FileOutputStream(target);
FileChannel cfis=fis.getChannel();
FileChannel cfos=fos.getChannel();
cfis.transferTo(0, fis.available(), cfos);
}
public static void copy(FileInputStream fis,File target) throws IOException{
FileOutputStream fos=new FileOutputStream(target);
FileChannel cfis=fis.getChannel();
FileChannel cfos=fos.getChannel();
cfis.transferTo(0, fis.available(), cfos);
}
public static void copy(File source,FileOutputStream fos) throws IOException{
FileInputStream fis=new FileInputStream(source);
FileChannel cfis=fis.getChannel();
FileChannel cfos=fos.getChannel();
cfis.transferTo(0, fis.available(), cfos);
}
}