0x01:
FileInputStream/FileOutputStream字节流进行文件的复制
0x02:
BufferedInputStream/BufferedOutputStream高效字节流进行复制文件
0x03: FileReader/FileWriter字符流进行文件复制文件
private static void readerWriterCopyFile(File srcFile, File desFile){ try{ // 使用字符流进行文件复制,注意:字符流只能复制只含有汉字的文件 FileReader fr = new FileReader(srcFile); FileWriter fw = new FileWriter(desFile); Integer by = 0; while((by = fr.read()) != -1) { fw.write(by); } fr.close(); fw.close(); }catch(Exception e){ e.printStackTrace(); }}0x04:
BufferedReader/BufferedWriter高效字符流进行文件复制
0x05: NIO实现文件拷贝(用transferTo的实现 或者transferFrom的实现)
public static void NIOCopyFile(String source,String target) { try{ //1.采用RandomAccessFile双向通道完成,rw表示具有读写权限 RandomAccessFile fromFile = new RandomAccessFile(source,"rw"); FileChannel fromChannel = fromFile.getChannel(); RandomAccessFile toFile = new RandomAccessFile(target,"rw"); FileChannel toChannel = toFile.getChannel(); long count = fromChannel.size(); while (count > 0) { long transferred = fromChannel.transferTo(fromChannel.position(), count, toChannel); count -= transferred; } if(fromFile!=null) { fromFile.close(); } if(fromChannel!=null) { fromChannel.close(); } }catch(Exception e){ e.printStackTrace(); }}0x06: java.nio.file.Files.copy()实现文件拷贝,其中第三个参数决定是否覆盖
public static void copyFile(String source,String target){ Path sourcePath = Paths.get(source); Path destinationPath = Paths.get(target); try { Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { e.printStackTrace(); }}