package com.jackie.io.readfile; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class CopyFile { public static void main(String[] args) { //建立联系 确保源文件必须存在 //目标文件可以不存在 String srcPath = "E:/workspace/newFile.txt"; String destPath = "E:/workspace/destFile.txt"; File src = new File(srcPath); File dest = new File(destPath); //选择流 InputStream is = null; OutputStream os = null; byte[] byteArr = new byte[1024]; int len = 0; try { //操作 is = new FileInputStream(src); os = new FileOutputStream(dest); while(-1 != (len =is.read(byteArr))){ os.write(byteArr, 0, len); } os.flush(); } catch (IOException e) { e.printStackTrace(); } finally { //释放资源 if(os != null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(is != null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } }