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
stocksami@earthlink.net - 30 Mar 2006 19:27 GMT
> 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
[quoted text clipped - 6 lines]
>
> 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
Oliver Wong - 30 Mar 2006 19:50 GMT
>> 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
[quoted text clipped - 7 lines]
> 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
Dave Mandelin - 30 Mar 2006 20:16 GMT
> >> 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
[quoted text clipped - 14 lines]
>
> - 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
Roedy Green - 30 Mar 2006 21:16 GMT
>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

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.