<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 用 Java 復制文件 原文:http://zetcode.com/java/copyfile/ 在 Java 復制文件教程中,我們展示了如何使用 Java 復制文件。 我們復制具有內置類的文件,包括`File`,`FileInputStream`,`FileOutputStream`,`FileChannel`和`Files`。 我們還使用兩個第三方庫:Apache Commons IO 和 Google Guava。 文件復制是創建一個與現有文件具有相同內容的新文件。 文件移動正在將文件從一個位置傳輸到另一位置。 要復制的文件稱為源文件,而新副本稱為目標文件。 ## bugs 文件 在示例中,我們使用`bugs.txt`文件。 `bugs.txt` ```java Assasin bug, Avondale spider, Backswimmer, Bamboo moth, Banana moth, Bed bug, Black cocroach, Blue moon, Bumble Bee, Carpenter Bee, Cattle tick, Cave Weta, Cicada, Cinnibar, Click beetle, Clothes moth, Codling moth, Centipede, Earwig, Eucalypt longhorn beetle, Field Grasshopper, Garden slug, Garden soldier, German cockroach, German wasp, Giant dragonfly, Giraffe weevil, Grass grub, Grass looper, Green planthopper, Green house spider, Gum emperor, Gum leaf skeletoniser, Hornet, Mealybug, Mites, Mole Cricket, Monarch butterfly, Mosquito, Silverfish, Wasp, Water boatman, Winged weta, Wolf spider, Yellow Jacket, Yellow Admiral ``` 這是一個包含錯誤名稱的簡單文件。 ## 使用`FileInputStream`和`FileOutputStream`復制文件 使用`FileInputStream`和`FileOutputStream`,我們創建用于讀取和寫入`File`的流。 找不到文件時,將引發`FileNotFoundException`。 `File`是 Java 中文件或目錄的表示。 `com/zetcode/CopyFileStream.java` ```java package com.zetcode; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyFileStream { public static void main(String[] args) throws IOException { var source = new File("src/resources/bugs.txt"); var dest = new File("src/resources/bugs2.txt"); try (var fis = new FileInputStream(source); var fos = new FileOutputStream(dest)) { byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { fos.write(buffer, 0, length); } } } } ``` 本示例使用`FileInputStream`,`FileOutputStream`和`File`復制文件。 ```java try (var fis = new FileInputStream(source); var fos = new FileOutputStream(dest)) { ``` 我們創建`FileInputStream`和`FileOutputStream`的實例。 `try-with-resources`語句將自動關閉流。 ```java byte[] buffer = new byte[1024]; ``` 我們將復制 1024 個字節的文本塊。 這樣做是為了獲得更好的性能。 ```java while ((length = is.read(buffer)) > 0) { ``` `FileInputStream`的`read()`方法從輸入流中讀取指定數量的字節,并將其存儲到緩沖區數組中。 它返回讀入緩沖區的字節總數,如果由于到達流的末尾而沒有更多數據,則返回 -1。 ```java os.write(buffer, 0, length); ``` `FileOutputStream`的`write()`方法將存儲在緩沖區中的字節寫入輸出流。 第一個參數是數據,第二個參數是數據中的起始偏移量,最后一個參數是要寫入的字節數。 ## 使用`Paths`和`Files`復制文件 下一個示例與上一個示例相似。 它使用了更現代的 API。 `com/zetcode/CopyFileStream2.java` ```java package com.zetcode; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class CopyFileStream2 { public static void main(String[] args) throws IOException { var source = Paths.get("src/resources/bugs.txt"); var dest = Paths.get("src/resources/bugs2.txt"); try (var fis = Files.newInputStream(source); var fos = Files.newOutputStream(dest)) { byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { fos.write(buffer, 0, length); } } } } ``` 我們使用`Paths`和`Files`類復制文件。 ```java var source = Paths.get("src/resources/bugs.txt"); var dest = Paths.get("src/resources/bugs2.txt"); ``` 從文件中,我們創建`Path`對象。 ```java try (var fis = Files.newInputStream(source); var fos = Files.newOutputStream(dest)) { ``` 流是在`Files`類的幫助下創建的。 ## 使用`FileChannel`的復制文件 `FileChannel`是用于讀取,寫入,映射和操作文件的通道。 `FileChannel`是經典 Java IO 流 API 的替代方法。 它位于`java.nio`包中。 `RandomAccessFile`支持讀取和寫入隨機訪問文件。 隨機訪問意味著我們可以在文件中的任何位置讀取或寫入信息。 `com/zetcode/CopyFileChannel.java` ```java package com.zetcode; import java.io.IOException; import java.io.RandomAccessFile; public class CopyFileChannel { public static void main(String[] args) throws IOException { var source = new RandomAccessFile("src/resources/bugs.txt", "r"); var dest = new RandomAccessFile("src/resources/bugs2.txt", "rw"); try (var sfc = source.getChannel(); var dfc = dest.getChannel()) { dfc.transferFrom(sfc, 0, sfc.size()); } } } ``` 該示例使用`FileChannel`復制文本文件。 ```java var source = new RandomAccessFile("src/resources/bugs.txt", "r"); ``` 創建讀取模式下的隨機訪問文件。 ```java var dest = new RandomAccessFile("src/resources/bugs2.txt", "rw"); ``` 創建一個處于讀/寫模式的隨機訪問文件。 ```java try (var sfc = source.getChannel(); var dfc = dest.getChannel()) { ``` 我們使用`getChannel()`從文件中獲取頻道。 ```java dfc.transferFrom(sfc, 0, sfc.size()); ``` `transferFrom()`方法將字節從源通道傳輸到目標通道。 第一個參數是源通道,第二個參數是文件中傳輸的開始位置,第三個參數是要傳輸的最大字節數。 ## 用`Files.copy`復制文件 Java 7 引入了`Files.copy()`方法,該方法提供了一種復制文件的簡便方法。 如果目標文件存在,則復制失敗,除非指定了`REPLACE_EXISTING`選項。 `Files.copy()`采用可選的第三副本選項參數。 `options`參數可以包括以下任何一項: * `REPLACE_EXISTING` - 如果目標文件存在,則目標文件不是非空目錄時,將替換目標文件。 * `COPY_ATTRIBUTES` - 嘗試將與此文件關聯的文件屬性復制到目標文件。 * `ATOMIC_MOVE` - 移動文件。 * `NOFOLLOW_LINKS` - 不遵循符號鏈接。 前三個選項在`StandarCopyOption`中可用; `LinkOption`中的最后一個。 `com/zetcode/CopyFileJava7.java` ```java package com.zetcode; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; public class CopyFileJava7 { public static void main(String[] args) throws IOException { var source = new File("src/resources/bugs.txt"); var dest = new File("src/resources/bugs2.txt"); Files.copy(source.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING); } } ``` 本示例使用`Files.copy()`復制文件。 如果目的地已經存在,它將替換目的地。 ## 使用 Apache Commons IO 進行 Java 復制文件 Apache Commons IO 是一個工具庫,可幫助開發 IO 功能。 它包含`FileUtils.copyFile()`方法來執行復制。 `FileUtils.copyFile()`將指定源文件的內容復制到指定目標文件中,并保留文件日期。 如果目標文件所在的目錄不存在,則創建該目錄。 如果目標文件存在,則此方法將覆蓋它。 ```java <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency> ``` 對于此示例,我們需要`commons-io`工件。 `com/zetcode/CopyFileApacheCommons.java` ```java package com.zetcode; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; public class CopyFileApacheCommons { public static void main(String[] args) throws IOException { var source = new File("src/main/resources/bugs.txt"); var dest = new File("src/main/resources/bugs2.txt"); FileUtils.copyFile(source, dest); } } ``` 該示例使用 Apache Commons 的`FileUtils.copyFile()`復制文件。 ## 用 Guava Java 復制文件 Google Guava 是 Java 通用庫的開源集。 它包括用于復制文件的`Files.copy()`。 ```java <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>28.0-jre</version> </dependency> ``` 我們使用 Guava 依賴。 `com/zetcode/CopyFileGuava.java` ```java package com.zetcode; import com.google.common.io.Files; import java.io.File; import java.io.IOException; public class CopyFileGuava { public static void main(String[] args) throws IOException { var source = new File("src/main/resources/bugs.txt"); var dest = new File("src/main/resources/bugs2.txt"); Files.copy(source, dest); } } ``` 該示例使用 Guava 的`Files.copy()`復制文件。 在本教程中,我們展示了幾種用 Java 復制文件的方法。 我們使用了內置工具和第三方庫。 您可能也對相關教程感興趣: [Java 文件教程](/java/file/), [Java 創建目錄](/java/createdirectory/), [Java 文件大小](/java/filesize/),[用 Java 創建文件](/java/createfile/) ,[用 Java 讀取文本文件](/articles/javareadtext/), [Apache `FileUtils`教程](/java/fileutils/), [Java 教程](/lang/java/)。 列出[所有 Java 教程](/all/#java)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看