How to read numerical data files

S

stocksami

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
 
S

stocksami

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
 
O

Oliver Wong

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
 
D

Dave Mandelin

Oliver said:
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.
 
R

Roedy Green

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top