Java: reading strings from a random access file with buffered input

后端 未结 7 1810
醉话见心
醉话见心 2020-12-14 12:40

I\'ve never had close experiences with Java IO API before and I\'m really frustrated now. I find it hard to believe how strange and complex it is and how hard it could be to

7条回答
  •  有刺的猬
    2020-12-14 13:23

    Start with a RandomAccessFile and use read or readFully to get a byte array between pos1 and pos2. Let's say that we've stored the data read in a variable named rawBytes.

    Then create your BufferedReader using

    new BufferedReader(new InputStreamReader(new ByteArrayInputStream(rawBytes)))
    

    Then you can call readLine on the BufferedReader.

    Caveat: this probably uses more memory than if you could make the BufferedReader seek to the right location itself, because it preloads everything into memory.

提交回复
热议问题