java nio 大文件,使用Java NIO读取和写入大文件

How can I effectively read from a large file and write bulk data into a file using the Java NIO framework.

I'm working with ByteBuffer and FileChannel and had tried something like below:

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

}

}

Can anybody tell, should I follow the same procedure if my file size in more than 2 GB?

What should I follow if the similar things I want to do while writing if written operations are in bulk?

解决方案

Note that you can simply use Files.copy(Paths.get(inFileStr),Paths.get(outFileStr), StandardCopyOption.REPLACE_EXISTING) to copy the file as your example code does, just likely faster and with only one line of code.

Otherwise, if you already have opened the two file channels, you can just use

in.transferTo(0, in.size(), out) to transfer the entire contents of the in channel to the out channel. Note that this method allows to specify a range within the source file that will be transferred to the target channel’s current position (which is initially zero) and that there’s also a method for the opposite way, i.e. out.transferFrom(in, 0, in.size()) to transfer data from the source channel’s current position to an absolute range within the target file.

Together, they allow almost every imaginable nontrivial bulk transfer in an efficient way without the need to copy the data into a Java side buffer. If that’s not solving your needs, you have to be more specific in your question.

By the way, you can open a FileChannel directly without the FileInputStream/FileOutputStream detour since Java 7.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值