日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Java 文件及文件夹复制

發布時間:2025/3/21 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 文件及文件夹复制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。