java位元組流,批量拷貝!效率最高!
/**
* 檔案拷貝,位元組批量讀取
* @param srcFile
* @param destFile
* @throws IOException
*/
public static void copyFile(File srcFile,File destFile)throws IOException{//流一般都有io錯誤
if(!srcFile.exists()){
throw new IllegalArgumentException(“檔案:”+srcFile+”不存在”);
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+”不是檔案”);
}
FileInputStream in = new FileInputStream(srcFile);//讀取 srcFile裡的檔案
FileOutputStream out = new FileOutputStream(destFile);//寫入destFile中
byte[] buf = new byte[8*1024];
int b ;
while((b = in.read(buf,0,buf.length))!=-1){//讀到buf中
out.write(buf);//寫入
out.flush();//最好加上重新整理
}
in.close();
out.close();
}
public static void main(String[] args) {
try {
copyFile(new File(“D:\test\rat.src”), new File(“D:\test\rat.src”));//要用類名呼叫
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}