<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java 文件復制 – 用 Java 復制文件的 4 種方法 > 原文: [https://howtodoinjava.com/java/io/4-ways-to-copy-files-in-java/](https://howtodoinjava.com/java/io/4-ways-to-copy-files-in-java/) **在 Java** 中將文件從一個位置復制到另一個位置是您在應用程序中需要執行的常見任務。 在此示例中,我列出了 4 種不同的方式來**復制 Java** 中的文件。 使用最適合您需要的一種。 ## 1)使用 apache commons IO 復制文件 要使用此方法,您需要[下載 apache 通用 IO 的 jar](https://commons.apache.org/proper/commons-io/download_io.cgi "download apache common io") 文件,并將其作為依賴項包含在您的項目中。 ```java private static void fileCopyUsingApacheCommons() throws IOException { File fileToCopy = new File("c:/temp/testoriginal.txt"); File newFile = new File("c:/temp/testcopied.txt"); FileUtils.copyFile(fileToCopy, newFile); // OR IOUtils.copy(new FileInputStream(fileToCopy), new FileOutputStream(newFile)); } ``` ## 2)使用`java.nio.file.Files.copy()`復制文件 這種方法非常快速且易于編寫。 ```java private static void fileCopyUsingNIOFilesClass() throws IOException { Path source = Paths.get("c:/temp/testoriginal.txt"); Path destination = Paths.get("c:/temp/testcopied.txt"); Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING); } ``` ## 3)使用`java.nio.channels.FileChannel.transferTo()`復制文件 如果您喜歡通道類的出色表現,請使用此方法。 ```java private static void fileCopyUsingNIOChannelClass() throws IOException { File fileToCopy = new File("c:/temp/testoriginal.txt"); FileInputStream inputStream = new FileInputStream(fileToCopy); FileChannel inChannel = inputStream.getChannel(); File newFile = new File("c:/temp/testcopied.txt"); FileOutputStream outputStream = new FileOutputStream(newFile); FileChannel outChannel = outputStream.getChannel(); inChannel.transferTo(0, fileToCopy.length(), outChannel); inputStream.close(); outputStream.close(); } ``` ## 4)使用`FileStreams`復制文件 如果您對較舊的 Java 版本感到震驚,那么這個適合您。 ```java private static void fileCopyUsingFileStreams() throws IOException { File fileToCopy = new File("c:/temp/testoriginal.txt"); FileInputStream input = new FileInputStream(fileToCopy); File newFile = new File("c:/temp/testcopied.txt"); FileOutputStream output = new FileOutputStream(newFile); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = input.read(buf)) > 0) { output.write(buf, 0, bytesRead); } input.close(); output.close(); } ``` ## 完整的示例代碼 ```java package com.howtodoinjava.examples.io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; public class FileCopyExamples { public static void main(String[] args) throws IOException { fileCopyUsingApacheCommons(); fileCopyUsingNIOFilesClass(); fileCopyUsingNIOChannelClass(); fileCopyUsingFileStreams(); } private static void fileCopyUsingFileStreams() throws IOException { File fileToCopy = new File("c:/temp/testoriginal.txt"); FileInputStream input = new FileInputStream(fileToCopy); File newFile = new File("c:/temp/testcopied.txt"); FileOutputStream output = new FileOutputStream(newFile); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = input.read(buf)) > 0) { output.write(buf, 0, bytesRead); } input.close(); output.close(); } private static void fileCopyUsingNIOChannelClass() throws IOException { File fileToCopy = new File("c:/temp/testoriginal.txt"); FileInputStream inputStream = new FileInputStream(fileToCopy); FileChannel inChannel = inputStream.getChannel(); File newFile = new File("c:/temp/testcopied.txt"); FileOutputStream outputStream = new FileOutputStream(newFile); FileChannel outChannel = outputStream.getChannel(); inChannel.transferTo(0, fileToCopy.length(), outChannel); inputStream.close(); outputStream.close(); } private static void fileCopyUsingApacheCommons() throws IOException { File fileToCopy = new File("c:/temp/testoriginal.txt"); File newFile = new File("c:/temp/testcopied.txt"); FileUtils.copyFile(fileToCopy, newFile); // OR IOUtils.copy(new FileInputStream(fileToCopy), new FileOutputStream(newFile)); } private static void fileCopyUsingNIOFilesClass() throws IOException { Path source = Paths.get("c:/temp/testoriginal.txt"); Path destination = Paths.get("c:/temp/testcopied.txt"); Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING); } } ``` **祝您學習愉快!**
                  <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>

                              哎呀哎呀视频在线观看