1. 程式人生 > >Java復制文件

Java復制文件

exception dir exceptio parent 文件 tput [] void IT

 1  public void copyFile(File fromFile,File toFile) throws IOException{
 2         //如果目標文件不存在,則創建
 3         File parentFile = toFile.getParentFile();
 4         if(!parentFile.exists()){
 5             parentFile.mkdirs();
 6         }
 7         if(!toFile.exists()){
 8             toFile.createNewFile();
9 } 10 //復制文件 11 FileInputStream ins = new FileInputStream(fromFile); 12 FileOutputStream out = new FileOutputStream(toFile); 13 byte[] b = new byte[1024]; 14 int n=0; 15 while((n=ins.read(b))!=-1){ 16 out.write(b, 0, n); 17 } 18
19 ins.close(); 20 out.close(); 21 }

Java復制文件