鍍金池/ 教程/ Java/ Java NIO通道之間的數(shù)據(jù)傳輸
Java NIO選擇器
Java NIO ServerSocketChannel
Java NIO通道FileLock
Java NIO組件
Java NIO編碼和解碼
Java NIO包
Java NIO緩沖區(qū)
Java NIO教程
Java NIO SocketChannel
Java NIO時(shí)間服務(wù)器示例
Java NIO字符集
Java NIO通道之間的數(shù)據(jù)傳輸
Java NIO通道
Java IO與NIO比較
Java NIO管道
Java NIO分散/聚集或向量I/O

Java NIO通道之間的數(shù)據(jù)傳輸

在Java NIO中,可以非常頻繁地將數(shù)據(jù)從一個(gè)通道傳輸?shù)搅硪粋€(gè)通道。批量傳輸文件數(shù)據(jù)是非常普遍的,因?yàn)閹讉€(gè)優(yōu)化方法已經(jīng)添加到FileChannel類(lèi)中,使其更有效率。

通道之間的數(shù)據(jù)傳輸在FileChannel類(lèi)中的兩種方法是:

  • FileChannel.transferTo()方法
  • FileChannel.transferFrom()方法

FileChannel.transferTo()方法

transferTo()方法用來(lái)從FileChannel到其他通道的數(shù)據(jù)傳輸。

下面來(lái)看一下transferTo()方法的例子:

public abstract class Channel extends AbstractChannel  
{    
   public abstract long transferTo (long position, long count, WritableByteChannel target);  
}

FileChannel.transferFrom()方法

transferFrom()方法允許從源通道到FileChannel的數(shù)據(jù)傳輸。

下面來(lái)看看transferFrom()方法的例子:

public abstract class Channel extends AbstractChannel  
{    
    public abstract long transferFrom (ReadableByteChannel src, long position, long count);  
}

基本通道到通道數(shù)據(jù)傳輸示例

下面來(lái)看看從4個(gè)不同文件讀取文件內(nèi)容的簡(jiǎn)單示例,并將它們的組合輸出寫(xiě)入第五個(gè)文件:

package com.yiibai;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.nio.channels.WritableByteChannel;
import java.nio.channels.FileChannel;

public class TransferDemo {
    public static void main(String[] argv) throws Exception {
        String relativelyPath = System.getProperty("user.dir");
        // Path of Input files
        String[] iF = new String[] { relativelyPath + "/input1.txt", relativelyPath + "/input2.txt",
                relativelyPath + "/input3.txt", relativelyPath + "/input4.txt" };
        // Path of Output file and contents will be written in this file
        String oF = relativelyPath + "/combine_output.txt";
        // Acquired the channel for output file
        FileOutputStream output = new FileOutputStream(new File(oF));
        WritableByteChannel targetChannel = output.getChannel();
        for (int j = 0; j < iF.length; j++) {
            // Get the channel for input files
            FileInputStream input = new FileInputStream(iF[j]);
            FileChannel inputChannel = input.getChannel();

            // The data is tranfer from input channel to output channel
            inputChannel.transferTo(0, inputChannel.size(), targetChannel);

            // close an input channel
            inputChannel.close();
            input.close();
        }
        // close the target channel
        targetChannel.close();
        output.close();
        System.out.println("All jobs done...");
    }
}

在上述程序中,將4個(gè)不同的文件(即input1.txt,input2.txt,input3.txtinput4.txt)的內(nèi)容讀取并將其組合的輸出寫(xiě)入第五個(gè)文件,即:combine_output.txt文件的中。combine_output.txt文件的內(nèi)容如下 -

this is content from input1.txt
this is content from input2.txt
this is content from input3.txt
this is content from input4.txt