通道之间的数据传输 – Java NIO

通道之间的数据传输 – Java NIO

原文: 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

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);
}

您无法在套接字通道之间进行直接传输,但是套接字通道实现了WritableByteChannelReadableByteChannel,因此可以使用transferTo()将文件的内容传输到套接字,或者可以使用transferFrom()从套接字直接读取数据到文件中。

另外,请记住,如果在传输过程中遇到问题,这些方法可能会抛出java.io.IOException

阅读更多: NIO 通道

通道之间的传输可能非常快,尤其是在底层操作系统提供本机支持的情况下。 某些操作系统可以执行直接传输,而无需在用户空间中传递数据。 对于大量数据传输而言,这可能是一个巨大的胜利。

通道间数据传输示例

在此示例中,我从 3 个不同的文件中读取文件内容,并将它们的合并输出写入第四个文件。

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();
	}
}

将您的评论和建议放在评论部分。

祝您学习愉快!