Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   How to read numerical data files (http://www.velocityreviews.com/forums/t151687-how-to-read-numerical-data-files.html)

stocksami@earthlink.net 03-30-2006 06:17 PM

How to read numerical data files
 
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 03-30-2006 06:27 PM

Re: How to read numerical data files
 

stocksami@earthlink.net 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


Oliver Wong 03-30-2006 06:50 PM

Re: How to read numerical data files
 

<stocksami@earthlink.net> wrote in message
news:1143743231.808632.247800@z34g2000cwc.googlegr oups.com...
>
> stocksami@earthlink.net 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


Dave Mandelin 03-30-2006 07:16 PM

Re: How to read numerical data files
 
Oliver Wong wrote:
> <stocksami@earthlink.net> wrote in message
> news:1143743231.808632.247800@z34g2000cwc.googlegr oups.com...
> >
> > stocksami@earthlink.net 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


Roedy Green 03-30-2006 08:16 PM

Re: How to read numerical data files
 
On 30 Mar 2006 10:17:53 -0800, stocksami@earthlink.net 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.


All times are GMT. The time now is 10:10 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.