Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading large (450000+ chars) strings from file

So, I'm dealing with integrating a legacy system. It produces a large text file, that prints instructions in one large string. Really large string. We're talking 450 000 characters or more.

I need to break this up in to lines, one per instruction. Each instruction is separated by a five digit code, where the code contains the number of characters in the next instruction.

My solution is writing a small java program that uses a buffered reader to read the file into a string, which is subsequently split into lines, and saved to a new file.

Any advice on handling this? Will a buffered reader be able to read this into a regular string? Am i doing this wrong?

like image 863
Eric Olsvik Avatar asked Apr 17 '15 13:04

Eric Olsvik


1 Answers

Yes. Use a buffered reader.

Work out the max size of an instruction and create a char[] of that size. Then do something like:

 reader.read(charArray, 0, 5);

 // parse the header

 reader.read(charArray, 0, lengthOfInstruction);

 String instruction = new String(charArray, 0, lengthOfInstruction);

 // do stuff with the instruction

You put this in a while loop that terminates when the file ends.

This might not be the most run-time efficient, but it's probably good enough and will be simple enough to get working.

like image 50
Ashley Frieze Avatar answered Sep 29 '22 15:09

Ashley Frieze