Java NIO
フレームワークを使用して、大きなファイルから効果的に読み込み、バルクデータをファイルに書き込むにはどうすればよいですか。Java NIOを使用して大きなファイルを読み書きする
私はByteBuffer
とFileChannel
で働いていると、以下のようなものを試してみました:
public static void main(String[] args)
{
String inFileStr = "screen.png";
String outFileStr = "screen-out.png";
long startTime, elapsedTime;
int bufferSizeKB = 4;
int bufferSize = bufferSizeKB * 1024;
// Check file length
File fileIn = new File(inFileStr);
System.out.println("File size is " + fileIn.length() + " bytes");
System.out.println("Buffer size is " + bufferSizeKB + " KB");
System.out.println("Using FileChannel with an indirect ByteBuffer of " + bufferSizeKB + " KB");
try ( FileChannel in = new FileInputStream(inFileStr).getChannel();
FileChannel out = new FileOutputStream(outFileStr).getChannel())
{
// Allocate an indirect ByteBuffer
ByteBuffer bytebuf = ByteBuffer.allocate(bufferSize);
startTime = System.nanoTime();
int bytesCount = 0;
// Read data from file into ByteBuffer
while ((bytesCount = in.read(bytebuf)) > 0) {
// flip the buffer which set the limit to current position, and position to 0.
bytebuf.flip();
out.write(bytebuf); // Write data from ByteBuffer to file
bytebuf.clear(); // For the next read
}
elapsedTime = System.nanoTime() - startTime;
System.out.println("Elapsed Time is " + (elapsedTime/1000000.0) + " msec");
}
catch (IOException ex) {
ex.printStackTrace();
}
}
誰もがあれば、私は、同じ手順に従うべきで、言うことができる2 GB以上の私のファイルサイズは?
書面による操作が一括して行われているのと同様のことを書いていたら、私は何をしたいのですか?
以来
FileInputStream
/FileOutputStream
迂回せずにopen aFileChannel
directlyをしようとしていますすることができ、またはあなたはそれをコピーしたいのですか? – Kayaman「私がしたいと思っているものが類似している」とはどういう意味ですか? – EJP