IO流-文件拷贝
其实文件的拷贝还是文件读取写入的应用,实际是读取此路径上的文件,然后写入到指定路径下的文件。
代码举例:
import java.io.*; import java.lang.*; class Test { public static void main(String[] args) { copy("C:\\log.txt", "d:\\log.txt"); } //块读取方式 public static void copy(String sourcePath, String destPath) { final int READCACHELEN = 1024; try(FileReader fileReader = new FileReader(sourcePath)) { int readReturnNum = 0; char[] readCache = new char[READCACHELEN]; try(FileWriter fileWriter = new FileWriter(destPath, true))//定义添加为真 { while((readReturnNum = fileReader.read(readCache)) != -1) { fileWriter.write(readCache, 0, readReturnNum); } } catch(Exception e) { e.printStackTrace(); } } catch(Exception e) { e.printStackTrace(); } System.out.println("transport Ok!"); } //单个字符读取方式 public static void copy(String sourcePath, String destPath) { final int READCACHELEN = 1024; try(FileReader fileReader = new FileReader(sourcePath)) { int readReturnNum = 0; try(FileWriter fileWriter = new FileWriter(destPath, true)) { while((readReturnNum = fileReader.read()) != -1)//read读取单个字符以整型返回 { fileWriter.write(readReturnNum); } } catch(Exception e) { e.printStackTrace(); } } catch(Exception e) { e.printStackTrace(); } System.out.println("transport Ok!"); } }