Java - is it possible to read a file line by line, stop and then immediately start reading bytes where I left off?

I am having a problem parsing the ascii part of the file, and as soon as I click on the end of the tag, IMMEDIATELY start reading in bytes from now. All I know in Java for reading a string or whole word is creating a buffer that ruins any chance of getting bytes right after my breakpoint. This is the only way to do it in bytes, find newlines, restore everything to a newline, see if this is my end tag, and from there?

+2


source to share


5 answers


How big is this file? My first thought is to read the whole thing into a ByteBuffer or ByteArrayOutputStream without trying to process it, and then find the tag by comparing the byte values. Once you know where the text part ends and the binary part begins, you process each part as needed.



+1


source


Perhaps, but as far as I know, not with classes from the API.



You can do it manually - open it as a BufferedInputStream that supports mark

/ reset

. You read block by block ( byte[]

) and you parse it as ASCII. Eventually you accumulate it in a buffer until you hit the marker. But before you you read

are calling mark

. If you think you are reading whatever you want in ASCII, you call reset

and then call read

to unload the rest of the ASCII. And now you have BufferedInputStream

(which is InputStream

) ready to read the binary portion of the file.

+2


source


I think the best idea would be to abandon the concept of "lines". To find the end tag, create a circular buffer that is large enough to contain the end tag, read it byte by byte and check after each byte to see if it contains a tag.

There are more sophisticated and efficient search algorithms, but the difference only matters for longer searches (presumably your end tag is short).

+2


source


Yes, you are correct byte by byte. Abstraction has its drawbacks.

0


source


Is the file growing or is it static?

If it is static see http://java.sun.com/javase/6/docs/api/java/nio/MappedByteBuffer.html

0


source







All Articles