java file input with rewind()/reset() capability

后端 未结 9 1602
时光说笑
时光说笑 2020-12-03 00:53

I need to write a function that takes in some kind of input stream thing (e.g. an InputStream or a FileChannel) in order to read a large file in two passes: once to precompu

相关标签:
9条回答
  • 2020-12-03 01:42

    PushbackInputStream will also work, as long as you know how many characters you want to be able to rewind

    0 讨论(0)
  • 2020-12-03 01:44

    RandomAccessFile is what you want:

    • fseek() is translated to RandomAccessFile#seek
    • ftell() is translated to RandomAccessFile#getFilePointer
    • rewind() is seek(0)
    0 讨论(0)
  • 2020-12-03 01:44

    BufferedInputStream has mark(readlimit) and reset(). readlimit should be larger than filesize to make mark valid. file.length()+1 is OK. This means mark is valid until readlimit bytes are read, thus you can go back by reset().

    0 讨论(0)
提交回复
热议问题