Reading file chunk by chunk

前端 未结 3 1920
名媛妹妹
名媛妹妹 2020-12-02 00:50

I want to read a file piece by piece. The file is split up into several pieces which are stored on different types of media. What I currently do is call each seperate piece

3条回答
  •  囚心锁ツ
    2020-12-02 01:05

    Instead of older io you can try nio for reading file chunk by chunk in memory not full file . You can use Channel to get datas from multiple source

    RandomAccessFile aFile = new RandomAccessFile(
                            "test.txt","r");
            FileChannel inChannel = aFile.getChannel();
            long fileSize = inChannel.size();
            ByteBuffer buffer = ByteBuffer.allocate((int) fileSize);
            inChannel.read(buffer);
            //buffer.rewind();
            buffer.flip();
            for (int i = 0; i < fileSize; i++)
            {
                System.out.print((char) buffer.get());
            }
            inChannel.close();
            aFile.close();
    

提交回复
热议问题