搜尋
首頁JavaJava基礎java怎麼複製檔案?

java怎麼複製檔案?

Dec 03, 2019 pm 01:35 PM
java

java怎麼複製檔案?

java複製檔案的方法:(推薦:java影片教學

一、使用FileStreams複製

這是最經典的方式將一個檔案的內容複製到另一個檔案中。使用FileInputStream讀取檔案A的位元組,使用FileOutputStream寫入到檔案B。這是第一個方法的程式碼:

private static void copyFileUsingFileStreams(File source, File dest)
        throws IOException {    
    InputStream input = null;    
    OutputStream output = null;    
    try {
           input = new FileInputStream(source);
           output = new FileOutputStream(dest);        
           byte[] buf = new byte[1024];        
           int bytesRead;        
           while ((bytesRead = input.read(buf)) != -1) {
               output.write(buf, 0, bytesRead);
           }
    } finally {
        input.close();
        output.close();
    }
}

正如你所看到的我們執行幾個讀和寫操作try的資料,所以這應該是一個低效率的,下一個方法我們將看到新的方式。

二、使用FileChannel複製

Java NIO包含transferFrom方法,根據文件應該比文件流複製的速度更快。這是第二種方法的程式碼:

private static void copyFileUsingFileChannels(File source, File dest) throws IOException {    
        FileChannel inputChannel = null;    
        FileChannel outputChannel = null;    
    try {
        inputChannel = new FileInputStream(source).getChannel();
        outputChannel = new FileOutputStream(dest).getChannel();
        outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
    } finally {
        inputChannel.close();
        outputChannel.close();
    }
}

三、使用Commons IO複製

Apache Commons IO提供拷貝檔案方法在其FileUtils類別,可用於複製一個檔案到另一個地方。它非常方便使用Apache Commons FileUtils類別時,您已經使用您的專案。基本上,這個類別是使用Java NIO FileChannel內部。這是第三種方法的程式碼:

 private static void copyFileUsingApacheCommonsIO(File source, File dest)
         throws IOException {
     FileUtils.copyFile(source, dest);
 }

此方法的核心程式碼如下:

private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
        if (destFile.exists() && destFile.isDirectory()) {
            throw new IOException("Destination '" + destFile + "' exists but is a directory");
        }

        FileInputStream fis = null;
        FileOutputStream fos = null;
        FileChannel input = null;
        FileChannel output = null;
        try {
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            input  = fis.getChannel();
            output = fos.getChannel();
            long size = input.size();
            long pos = 0;
            long count = 0;
            while (pos  FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : size - pos;
                pos += output.transferFrom(input, pos, count);
            }
        } finally {
            IOUtils.closeQuietly(output);
            IOUtils.closeQuietly(fos);
            IOUtils.closeQuietly(input);
            IOUtils.closeQuietly(fis);
        }

        if (srcFile.length() != destFile.length()) {
            throw new IOException("Failed to copy full contents from '" +
                    srcFile + "' to '" + destFile + "'");
        }
        if (preserveFileDate) {
            destFile.setLastModified(srcFile.lastModified());
        }
    }

由此可見,使用Apache Commons IO複製檔案的原理就是上述第二種方法:使用FileChannel複製

四、使用Java7的Files類別複製

如果你有一些經驗在Java 7中你可能會知道,可以使用複製方法的Files類別檔案,從一個檔案複製到另一個檔案。這是第四個方法的程式碼:

 private static void copyFileUsingJava7Files(File source, File dest)
         throws IOException {    
         Files.copy(source.toPath(), dest.toPath());
}

更多java知識請關注java基礎教學欄位。

以上是java怎麼複製檔案?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Java中有哪些不同的垃圾收集算法(串行,並行,CMS,G1,ZGC)?Java中有哪些不同的垃圾收集算法(串行,並行,CMS,G1,ZGC)?Mar 14, 2025 pm 05:06 PM

本文討論了各種Java垃圾收集算法(串行,並行,CMS,G1,ZGC),它們的性能影響和適合大量堆的應用。

什麼是Java虛擬機(JVM),它在內部如何工作?什麼是Java虛擬機(JVM),它在內部如何工作?Mar 14, 2025 pm 05:05 PM

本文討論了Java虛擬機(JVM),詳細介紹了其在不同平台運行Java程序中的作用。它說明了JVM的內部流程,密鑰組件,內存管理,垃圾收集和性能Optimizatio

如何使用Java的Nashorn Engine用JavaScript腳本?如何使用Java的Nashorn Engine用JavaScript腳本?Mar 14, 2025 pm 05:00 PM

Java的Nashorn Engine可以在Java應用程序中啟用JavaScript腳本。關鍵步驟包括設置Nashorn,管理腳本和優化性能。主要問題涉及安全性,內存管理和未來兼容性

如何使用Java的Try-with-Resources語句進行自動資源管理?如何使用Java的Try-with-Resources語句進行自動資源管理?Mar 14, 2025 pm 04:59 PM

Java的Try-with-Resources通過自動關閉文件流或數據庫連接等資源來簡化資源管理,從而提高代碼可讀性和可維護性。

如何使用Java的枚舉來表示固定的值集?如何使用Java的枚舉來表示固定的值集?Mar 14, 2025 pm 04:57 PM

Java枚舉代表固定的值集,通過自定義方法和構造函數提供類型安全性,可讀性和其他功能。它們增強了代碼組織,可用於開關語句中以進行有效的價值處理。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境