Split File - Java/Linux

前端 未结 4 1808
情歌与酒
情歌与酒 2021-01-21 03:12

I have a large file contains nearly 250 million characters. Now, I want to split it into parts of each contains 30 million characters ( so first 8 parts will contains 30 million

4条回答
  •  一个人的身影
    2021-01-21 03:44

    You can try this. I have to used read/mode the first time as the file didn't exist at first. Youc an use read only as this code suggests.

    long start = System.nanoTime();
    long fileSize = 3200 * 1024 * 1024L;
    FileChannel raf = new RandomAccessFile("deleteme.txt", "r").getChannel();
    long midPoint = fileSize / 2 / 4096 * 4096;
    MappedByteBuffer buffer1 = raf.map(FileChannel.MapMode.READ_ONLY, 0, midPoint + 4096);
    MappedByteBuffer buffer2 = raf.map(FileChannel.MapMode.READ_ONLY, midPoint, fileSize - midPoint);
    long time = System.nanoTime() - start;
    System.out.printf("Took %.3f ms to map a file of %,d bytes long%n", time / 1e6, raf.size());
    

    This is running on a Window 7 x64 box with 4 GB of memory.

    Took 3.302 ms to map a file of 3,355,443,200 bytes long
    

提交回复
热议问题