<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 NIO > 原文: [https://howtodoinjava.com/java7/nio/java-nio-2-0-how-to-transfer-copy-data-between-channels/](https://howtodoinjava.com/java7/nio/java-nio-2-0-how-to-transfer-copy-data-between-channels/) 與通常在輸入源和輸出目標之間發生 IO 的普通 Java 應用程序一樣,在 NIO 中,您也可能需要非常頻繁地將數據從一個通道傳輸到另一通道。 文件數據從一個地方到另一個地方的批量傳輸非常普遍,以至于`FileChannel`類添加了兩種優化方法,以使其效率更高。 ## `FileChannel.transferTo()`和`FileChannel.transferFrom()`方法 `transferTo()`和`transferFrom()`方法使您可以將一個通道交叉連接到另一個通道,而無需通過中間緩沖區傳遞數據。 這些方法僅在`FileChannel`類上存在,因此,在通道間傳輸中涉及的通道之一必須是`FileChannel`。 ```java public abstract class FileChannel extends AbstractChannel implements ByteChannel, GatheringByteChannel, ScatteringByteChannel { // There are more other methods public abstract long transferTo (long position, long count, WritableByteChannel target); public abstract long transferFrom (ReadableByteChannel src, long position, long count); } ``` 您無法在套接字通道之間進行直接傳輸,但是套接字通道實現了`WritableByteChannel`和`ReadableByteChannel`,因此可以使用`transferTo()`將文件的內容傳輸到套接字,或者可以使用`transferFrom()`從套接字直接讀取數據到文件中。 另外,請記住,如果在傳輸過程中遇到問題,這些方法可能會拋出`java.io.IOException`。 > **閱讀更多: [NIO 通道](//howtodoinjava.com/java-7/nio/java-nio-2-0-channels/ "Java NIO 2.0 : Channels")** **通道之間的傳輸可能非常快,尤其是在底層操作系統提供本機支持的情況下。 某些操作系統可以執行直接傳輸,而無需在用戶空間中傳遞數據。 對于大量數據傳輸而言,這可能是一個巨大的勝利。** ## 通道間數據傳輸示例 在此示例中,我從 3 個不同的文件中讀取文件內容,并將它們的合并輸出寫入第四個文件。 ```java package com.howtodoinjava.nio; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.channels.FileChannel; import java.nio.channels.WritableByteChannel; public class ChannelTransferExample { public static void main(String[] argv) throws Exception { //Input files String[] inputFiles = new String[]{"inputFile1.txt","inputFile2.txt","inputFile3.txt"}; //Files contents will be written in these files String outputFile = "outputFile.txt"; //Get channel for output file FileOutputStream fos = new FileOutputStream(new File(outputFile)); WritableByteChannel targetChannel = fos.getChannel(); for (int i = 0; i < inputFiles.length; i++) { //Get channel for input files FileInputStream fis = new FileInputStream(inputFiles[i]); FileChannel inputChannel = fis.getChannel(); //Transfer data from input channel to output channel inputChannel.transferTo(0, inputChannel.size(), targetChannel); //close the input channel inputChannel.close(); fis.close(); } //finally close the target channel targetChannel.close(); fos.close(); } } ``` 將您的評論和建議放在評論部分。 **祝您學習愉快!**
                  <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>

                              哎呀哎呀视频在线观看