Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

How to read numerical data files

Thread view: 
stocksami@earthlink.net - 30 Mar 2006 19:17 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
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.