JAVA NIO 大文件读取

时间:2023-01-18 20:15:48
Java代码 JAVA NIO 大文件读取 JAVA NIO 大文件读取JAVA NIO 大文件读取
  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. }