Results 1 to 5 of 5
  1. #1
    Simple_steve is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  3. #3
    Simple_steve is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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.

  4. #4
    Simple_steve is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: Reading text file into an array

    p.s. Thanks for replying so fast. :D

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Reading from a text file into an array
    By xyknight in forum New To Java
    Replies: 16
    Last Post: 04-10-2011, 11:10 PM
  2. Replies: 5
    Last Post: 03-12-2011, 01:04 AM
  3. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 12:51 AM
  4. Reading text file into an array
    By Mahesh_ps in forum New To Java
    Replies: 1
    Last Post: 10-09-2009, 03:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •