I'm having an issue trying to parse the ascii part of a file, and once I hit the end tag, IMMEDIATELY start reading in the bytes from that point on. Everything I know in Java to read off a line or a whole word creates a buffer, which ruins any chance of getting the bytes immediately following my stop point. Is the only way to do this read in byte-by-byte, find new-lines, reconstruct everything prior to the new-line, see if it's my end tag, and go from there?
views:
983answers:
6Yup, you're right about the byte-by-byte. Abstraction has its disadvantages.
It is possible, but as far as I know not with the classes from the API.
You can do it manually - open it as a BufferedInputStream, which 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 read
you call mark
. If you believe you read all you needed in ASCII, you call reset
and then you call read
to dump the rest of the ASCII part. And now you have a BufferedInputStream
(which is an InputStream
) ready for reading the binary part of the file.
I think the best idea would be to abandon the concept of "lines". To find the end tag, create a ring buffer that's just big enough to contain the end tag, read into it byte-by-byte, and after each byte check if it contains the tag.
There are more sophisticated and efficient search algorithms, but the difference is only relevant with longer search terms (presumably your end tag is short).
How big is this file? My first thought is to read the whole thing into a ByteBuffer or a ByteArrayOutputStream without trying to process it, then locate the tag by comparing byte values. Once you know where the text part ends and the binary part begins, you process each part as appropriate.
Is the file growing, or is it static?
If it's static, see http://java.sun.com/javase/6/docs/api/java/nio/MappedByteBuffer.html
I have two text file i want to compare there field ex. accno1 in first file accno2 in athoe if accno1=accno2 then action how can i do this opeartion in java i used IDE