原理:把原文件讀入到輸入流里,然后利用輸出流寫入到新的文件。
代碼如下:
/** * 復制文件 * @param fromFile * @param toFile * <br/> * 2016年12月19日 下午3:31:50 * @throws IOException */ public void copyFile(File fromFile,File toFile) throws IOException{ FileInputStream ins = new FileInputStream(fromFile); FileOutputStream out = new FileOutputStream(toFile); byte[] b = new byte[1024]; int n=0; while((n=ins.read(b))!=-1){ out.write(b, 0, n); } ins.close(); out.close(); }
限時領取免費Java相關資料,涵蓋了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo/Kafka、Hadoop、Hbase、Flink等高並發分布式、大數據、機器學習等技術。
資料傳送門:https://mp.weixin.qq.com/s/u2b_NVNuMuAPE0w4lc45fw
關注下方公眾號即可免費領取: