Results 1 to 5 of 5
Thread: Reading text file into an array
- 11-26-2011, 11:07 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
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.
- 11-27-2011, 12:14 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 11-27-2011, 12:14 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
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.
- 11-27-2011, 12:15 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
Re: Reading text file into an array
p.s. Thanks for replying so fast. :D
- 11-27-2011, 01:45 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Reading from a text file into an array
By xyknight in forum New To JavaReplies: 16Last Post: 04-10-2011, 11:10 PM -
Problem reading a text file into a two-dimensional character array
By wvu5004 in forum New To JavaReplies: 5Last Post: 03-12-2011, 01:04 AM -
[SOLVED] Reading a text file into an Array
By DonCash in forum New To JavaReplies: 13Last Post: 01-25-2011, 12:51 AM -
Reading text file into an array
By Mahesh_ps in forum New To JavaReplies: 1Last Post: 10-09-2009, 03:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks