Reading Strings and Binary from the same FileInputStream

前端 未结 7 2050
离开以前
离开以前 2021-01-17 22:29

I have a file that contains some amount of plain text at the start followed by binary content at the end. The size of the binary content is determined by some one of the pla

7条回答
  •  没有蜡笔的小新
    2021-01-17 22:48

    If you genuinely have a file (rather than something harder to seek in, e.g. a network stream) then I suggest something like this:

    • Open the file as a FileInputStream
    • Wrap it in InputStreamReader and a BufferedReader
    • Read the text, so you can find out how much content there is
    • Close the BufferedReader (which will close the InputStreamReader which will close the FileInputStream)
    • Reopen the file
    • Skip to (total file length - binary content length)
    • Read the rest of the data as normal

    You could just call mark() at the start of the FileInputStream and then reset() and skip() to get to the right place if you want to avoid reopening the file. (I was looking for an InputStream.seek() but I can't see one - I can't remember wanting it before in Java, but does it really not have one? Ick.)

提交回复
热议问题