JAVA NIO 大文件读取

Java代码 复制代码 收藏代码
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.RandomAccessFile;
  4. import java.nio.ByteBuffer;
  5. import java.nio.channels.FileChannel;
  6. public class TestNio {
  7. public static void main(String args[]) throws Exception {
  8. System.err.println("begin");
  9. long start = System.currentTimeMillis();
  10. int _5M = 1024*1024*5;
  11. File fin = new File("D:\\debug.log"); //文件大小200M
  12. File fout = new File("D:\\debug-bak.log");
  13. FileChannel fcin = new RandomAccessFile(fin, "r").getChannel();
  14. ByteBuffer rBuffer = ByteBuffer.allocate(_5M);
  15. FileChannel fcout = new RandomAccessFile(fout, "rws").getChannel();
  16. ByteBuffer wBuffer = ByteBuffer.allocateDirect(_5M);
  17. readFileByLine(_5M, fcin, rBuffer, fcout, wBuffer);
  18. System.err.print((System.currentTimeMillis() - start) /1000);
  19. }
  20. public static void readFileByLine(int bufSize, FileChannel fcin,
  21. ByteBuffer rBuffer, FileChannel fcout, ByteBuffer wBuffer) {
  22. String enterStr = "\n";
  23. try {
  24. byte[] bs = new byte[bufSize];
  25. StringBuilder strBuf = new StringBuilder("");
  26. String tempString = null;
  27. while (fcin.read(rBuffer) != -1) {
  28. int rSize = rBuffer.position();
  29. rBuffer.rewind();
  30. rBuffer.get(bs);
  31. rBuffer.clear();
  32. tempString = new String(bs, 0, rSize);
  33. int fromIndex = 0;
  34. int endIndex = 0;
  35. while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) {
  36. String line = tempString.substring(fromIndex, endIndex);
  37. line = strBuf.toString() + line;
  38. writeFileByLine(fcout, wBuffer, line);
  39. strBuf.delete(0, strBuf.length());
  40. fromIndex = endIndex + 1;
  41. }
  42. if (rSize > tempString.length()) {
  43. strBuf.append(tempString.substring(fromIndex,
  44. tempString.length()));
  45. } else {
  46. strBuf.append(tempString.substring(fromIndex, rSize));
  47. }
  48. }
  49. } catch (IOException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. public static void writeFileByLine(FileChannel fcout, ByteBuffer wBuffer,
  54. String line) {
  55. try {
  56. fcout.write(wBuffer.wrap(line.getBytes()), fcout.size());
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Java NIO 读取文件需要以下步骤: 1. 创建一个文件通道(FileChannel)对象,通过 FileInputStream 的 getChannel() 方法获取。 2. 创建一个字节缓冲区(ByteBuffer)对象,用于存放从文件读取的数据。 3. 调用通道的 read() 方法,将数据从文件读取到缓冲区中。 4. 循环读取数据,直到文件末尾。 5. 关闭文件通道和文件输入流。 下面是一个示例代码,演示如何使用 Java NIO 读取文件: ```java import java.io.FileInputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class NIOFileReader { public static void main(String[] args) throws Exception { FileInputStream inputStream = new FileInputStream("filename.txt"); FileChannel channel = inputStream.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); while (channel.read(buffer) != -1) { buffer.flip(); // 切换读模式 while (buffer.hasRemaining()) { System.out.print((char) buffer.get()); // 读取数据 } buffer.clear(); // 切换写模式 } channel.close(); inputStream.close(); } } ``` 在上面的代码中,我们首先创建了一个文件输入流(FileInputStream)对象,并通过 getChannel() 方法获取了一个文件通道(FileChannel)对象。然后我们创建了一个字节缓冲区(ByteBuffer)对象,并指定了缓冲区的大小为 1024 字节。接下来,我们使用 while 循环读取数据,每次读取一部分数据到缓冲区中,然后切换读模式,读取数据,再切换写模式,清空缓冲区。最后,我们关闭了文件通道和文件输入流。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值