【发布时间】:2020-11-13 15:17:21
【问题描述】:
我正在尝试使用 java NIO 流式传输大 CSV 文件,我能够从 CSV 文件中读取数据。请建议我们如何流式传输 CSV 文件的任何示例。我们需要在下面的代码中附加/更改什么代码。
请看下面的代码。
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ReadFileWithFixedSizeBuffer
{
public static void main(String[] args) throws IOException
{
RandomAccessFile aFile = new RandomAccessFile("airQuality.csv", "r");
FileChannel inChannel = aFile.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while(inChannel.read(buffer) > 0)
{
buffer.flip();
for (int i = 0; i < buffer.limit(); i++)
{
System.out.print((char) buffer.get());
}
buffer.clear(); // do something with the data and clear/compact it.
}
inChannel.close();
aFile.close();
}
}
任何帮助将不胜感激。 提前致谢!!
【问题讨论】: