Reading text file into an array
Hi, I have read a few posts about reading txt files into arrays, but none that are quite right for my needs.
I have a txt file full of 0's and 1's eg 00000100001000001110000000... and I need to read them into an array. I don't know how long the txt file is going to be. Help please i'm a bit new to this.
thanks
Steve.
Re: Reading text file into an array
It depends on circumstances - like how much data (roughly) you expect (1Kb!=1Tb) and what you are going to do with the data.
The Collections framework offers support for ... collections. In particular it provides data structures whose eventual size does not need to be known in advance. The List interface behaves like an array, but is not constrained to remain one set size. (And there are more modern collection classes like those in the Guava libraries. But if you haven't looked at the standard libraries, that is the place to start)
Another tack would be to use an array (bytes?, boolean?, char?) and estimate its size from the length() method of the File class.
Re: Reading text file into an array
well the file will be anywhere from 50 to around 1000 lines long, so we are not talking anywhere near a Tb, more along the lines of around a Mb. Ihad the idea that I would pass the data into a string, then use the .length to get the length of the array then pass that into an array, but I can't figure out how to do it. And seeing as it's in a string, how will the array recognise that each number 1 and 0 is individual and not one long array entry? I will then be parsing over the array with a loop checking each number saying if 1 then yes else No.
Re: Reading text file into an array
p.s. Thanks for replying so fast. :D
Re: Reading text file into an array
Does each line contain a fixed number of (binary) digits? If so, how many? If not, maybe a List<String> can do what you want, i.e. one String per line, all collected in a List.
kind regards,
Jos