Java 文件及文件夹复制
生活随笔
收集整理的這篇文章主要介紹了
Java 文件及文件夹复制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//?調用復制文件夾和文件的方法?
public static void main(String[] args) {
try { long startTime = System.currentTimeMillis();// 記錄開始時間 copyDir("F://fromFile", "E://toFile"); // 文件夾複製 copyFileUsingFileStreams("E://fromFile.doc", "F://toFile.doc"); // 文件複製 long endTime = System.currentTimeMillis();// 記錄結束時間 float excTime = (float) (endTime - startTime) / 1000; System.out.println("執行時間:" + excTime + "s"); // 輸出用時 } catch (IOException e) { System.out.println("失敗"); e.printStackTrace(); } }// 複製文件
private static void copyFileUsingFileStreams(String source, String dest) throws IOException { InputStream input = null; OutputStream output = null; try { input = new FileInputStream(new File(source)); output = new FileOutputStream(new File(dest)); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = input.read(buf)) != -1) { output.write(buf, 0, bytesRead); } } finally { input.close(); output.close(); } }?
// 複製文件夾 private static void copyDir(String sourcePath, String newPath) throws IOException { File file = new File(sourcePath); String[] filePath = file.list(); if (!(new File(newPath)).exists()) { new File(newPath)).mkdir(); } for (int i = 0; i < filePath.length; i++) { if ((new File(sourcePath + file.separator + filePath[i])).isDirectory()) { copyDir(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]); } if (new File(sourcePath + file.separator + filePath[i]).isFile()) { copyFileUsingFileStreams(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]); } } }轉載于:https://www.cnblogs.com/zhangzack/p/9511143.html
總結
以上是生活随笔為你收集整理的Java 文件及文件夹复制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Zabbix中文乱码问题
- 下一篇: Android基础(八) – Servi