<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 將文件復制到另一個文件 > 原文: [https://beginnersbook.com/2014/05/how-to-copy-a-file-to-another-file-in-java/](https://beginnersbook.com/2014/05/how-to-copy-a-file-to-another-file-in-java/) 在本教程中,我們將了解如何將一個文件的內容復制到 java 中的另一個文件中。為了復制文件,首先我們可以使用[`FileInputStream`](https://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html)讀取文件然后我們可以使用[`FileOutputStream`將讀取的內容寫入輸出文件](https://docs.oracle.com/javase/6/docs/api/java/io/FileOutputStream.html)。 ### 例 下面的代碼會將`MyInputFile.txt`的內容復制到`MyOutputFile.txt`文件中。如果`MyOutputFile.txt`不存在,則程序將首先創建文件,然后復制內容。 ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyExample { public static void main(String[] args) { FileInputStream instream = null; FileOutputStream outstream = null; try{ File infile =new File("C:\\MyInputFile.txt"); File outfile =new File("C:\\MyOutputFile.txt"); instream = new FileInputStream(infile); outstream = new FileOutputStream(outfile); byte[] buffer = new byte[1024]; int length; /*copying the contents from input stream to * output stream using read and write methods */ while ((length = instream.read(buffer)) > 0){ outstream.write(buffer, 0, length); } //Closing the input/output file streams instream.close(); outstream.close(); System.out.println("File copied successfully!!"); }catch(IOException ioe){ ioe.printStackTrace(); } } } ``` 輸出: ```java File copied successfully!! ``` 上述程序中使用的方法是: #### `read()`方法 ```java public int read(byte[] b) throws IOException ``` 將此輸入流中的`b.length`個字節數據讀入一個字節數組。此方法將阻止,直到某些輸入可用。它返回讀入緩沖區的總字節數,如果沒有更多數據,則返回 -1,因為已到達文件末尾。為了使這個方法在我們的程序中工作,我們創建了一個字節數組`buffer`并將輸入文件的內容讀取到相同的內容。由于此方法拋出`IOException`,因此我們將“讀取文件”代碼放在`try-catch`塊中以處理異常。 #### `write()`方法 ```java public void write(byte[] b, int off, int length) throws IOException ``` 將從偏移`off`開始的指定字節數組的長度字節寫入此文件輸出流。 **調整:** 如果輸入和輸出文件不在同一個驅動器中,則可以在創建文件對象時指定驅動器。例如,如果您的輸入文件在`C`盤中并且輸出文件在`D`盤中,那么您可以創建如下文件對象: ```java File infile =new File("C:\\MyInputFile.txt"); File outfile =new File("D:\\MyOutputFile.txt"); ```
                  <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>

                              哎呀哎呀视频在线观看