How to read numerical data files

Discussion in 'Java' started by stocksami@earthlink.net, Mar 30, 2006.

  1. Guest

    I'm learning Java while doing a real project and I'm having trouble
    with something that should be simple. I need to read text files that
    contain integer and floating point data separated by varying numbers of
    spaces. I need to store these values into arrays for later use. I've
    searched the web and can't find any simple examples. The files are
    quite large so the method needs to be reasonably efficient.

    Thanks

    Clark
    , Mar 30, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > I'm learning Java while doing a real project and I'm having trouble
    > with something that should be simple. I need to read text files that
    > contain integer and floating point data separated by varying numbers of
    > spaces. I need to store these values into arrays for later use. I've
    > searched the web and can't find any simple examples. The files are
    > quite large so the method needs to be reasonably efficient.
    >
    > Thanks
    >
    > Clark


    I guess I should add that I know how to use readline and to get an
    array of strings and then use the Float.valueof and Int.valueof
    methods. That doesn't seem efficient enough for the thousands of values
    that I need to read in.

    Clark
    , Mar 30, 2006
    #2
    1. Advertising

  3. Oliver Wong Guest

    <> wrote in message
    news:...
    >
    > wrote:
    >> I'm learning Java while doing a real project and I'm having trouble
    >> with something that should be simple. I need to read text files that
    >> contain integer and floating point data separated by varying numbers of
    >> spaces. I need to store these values into arrays for later use. I've
    >> searched the web and can't find any simple examples. The files are
    >> quite large so the method needs to be reasonably efficient.
    >>

    >
    > I guess I should add that I know how to use readline and to get an
    > array of strings and then use the Float.valueof and Int.valueof
    > methods. That doesn't seem efficient enough for the thousands of values
    > that I need to read in.


    Write it that way anyway.

    Only optimize if it *actually* becomes a problem; not merely if you
    *suspect* that it might become a problem.

    - Oliver
    Oliver Wong, Mar 30, 2006
    #3
  4. Oliver Wong wrote:
    > <> wrote in message
    > news:...
    > >
    > > wrote:
    > >> I'm learning Java while doing a real project and I'm having trouble
    > >> with something that should be simple. I need to read text files that
    > >> contain integer and floating point data separated by varying numbers of
    > >> spaces. I need to store these values into arrays for later use. I've
    > >> searched the web and can't find any simple examples. The files are
    > >> quite large so the method needs to be reasonably efficient.
    > >>

    > >
    > > I guess I should add that I know how to use readline and to get an
    > > array of strings and then use the Float.valueof and Int.valueof
    > > methods. That doesn't seem efficient enough for the thousands of values
    > > that I need to read in.

    >
    > Write it that way anyway.
    >
    > Only optimize if it *actually* becomes a problem; not merely if you
    > *suspect* that it might become a problem.
    >
    > - Oliver


    I second that. Thousands of lines doesn't seem like that many anyway.

    If you do end up needing to optimize, the fastest way I know of to do
    what you're doing is to read a big array of bytes at a time, and parse
    out the values using a finite state machine.

    --
    Want to play tabletop RPGs over the internet?
    Check out Koboldsoft RPZen: http://www.koboldsoft.com
    Dave Mandelin, Mar 30, 2006
    #4
  5. Roedy Green Guest

    On 30 Mar 2006 10:17:53 -0800, wrote, quoted
    or indirectly quoted someone who said :

    >with something that should be simple. I need to read text files that
    >contain integer and floating point data separated by varying numbers of
    >spaces. I need to store these values into arrays for later use. I've
    >searched the web and can't find any simple examples. The files are
    >quite large so the method needs to be reasonably efficient.


    there are three common ways:

    http://mindprod.com/jgloss/stringtokenizer.html
    http://mindprod.com/jgloss/streamtokenizer.html
    http://mindprod.com/jgloss/regex.html split method

    for converting Strings to double etc.
    see http://mindprod.com/applets/fileio.html

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Mar 30, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack
    Replies:
    5
    Views:
    365
    Dennis Lee Bieber
    Apr 1, 2006
  2. Wei-Chao Hsu

    How to read numerical data from a file

    Wei-Chao Hsu, Jul 11, 2004, in forum: C Programming
    Replies:
    3
    Views:
    268
    Al Bowers
    Jul 11, 2004
  3. Jack

    best way to index numerical text data ?

    Jack, Mar 31, 2006, in forum: C Programming
    Replies:
    1
    Views:
    328
    Jack Klein
    Mar 31, 2006
  4. Jack
    Replies:
    1
    Views:
    272
    Victor Bazarov
    Apr 1, 2006
  5. MrMF

    Numerical Integration of 2D data arrays

    MrMF, Nov 7, 2008, in forum: C Programming
    Replies:
    0
    Views:
    1,770
Loading...

Share This Page