Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-26-2007, 11:09 PM
Member
 
Join Date: Dec 2007
Posts: 3
sheetalnri is on a distinguished road
Need a solution to read and store data from a file
Hii everyone...I want to know how to store the input values which are read from a file. The input values are all double values in a line separated by spaces. I want to read these values one after the other and store in a double typed variable.

I actually wrote,
FileReader fr=new FileReader(filename);
BufferedReader in=new BufferedReader(fr);
while(true)
{
String line=in.readLine();//here i cud read the line

Please help me.
Thanks...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-26-2007, 11:46 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Reading a file
Hello

Your code is correct, but it will result in an infinite loop. To stop this, use a condition to break out of the loop. The following code will create a vector of String objects of each line in the file.
Code:
public static Vector<String> loadFile(String filename){ Vector<String> strings = new Vector<String>(); try{ FileReader file = new FileReader(filename); BufferedReader buffer = new BufferedReader(file); while (true) { String line = buffer.readLine(); if (line == null) break; else { strings.add(line); } } buffer.close(); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } return strings; }
Note that you must always close a file after using it and that the readLine() method will return null if there is no more to read. It is also important to handle any exceptions that by be thrown by the IO classes.


I do not have a IDE on this PC, so my code has been checked by hand.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-27-2007, 12:08 AM
Member
 
Join Date: Dec 2007
Posts: 3
sheetalnri is on a distinguished road
thanks for ur post...
but my question is how to store double values of a line of a text file one by one to variables...
if the first line of input is: 1.0,1.0,90.0
and second line: 1.0,1.0,0.0
and third line: ........... and so on

First I want to read the first line and then I want to store them like
double a=1.0;
double b=1.0;
double c=90.0;
and then go for the second line and read the data and store them
and so on....

thanks again...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-28-2007, 11:35 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Okay
Hello

I have attached a zip file for you. It contains an executable jar file, a "numbers.txt" file and the source code "Main.java". You can use a text editor to view the source code, like "Notepad" if you are using Windows. It can do everything that you asked and more. I like to use Vector data structures and iterators for fast coding. Just ask if you need more help. The program will increment all the numbers in the data file "numbers.txt". To see this:
  1. Open "numbers.txt"
  2. Close "numbers.txt"
  3. Run the jar executable
  4. Reopen "numbers.txt"

Just ask if you cannot access the code then I will post it.

Hope this helps.
Attached Files
File Type: zip example.zip (4.1 KB, 71 views)
__________________
If your ship has not come in yet then build a lighthouse.

Last edited by tim : 12-28-2007 at 11:37 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you read from a file, and then store the info in an array? szimme101 New To Java 5 07-30-2008 11:30 AM
How would you get information from a file and then store it in an array? szimme101 Advanced Java 3 04-07-2008 08:02 PM
How to store property file into key value pair Java Tip java.util 0 04-05-2008 12:16 PM
store file kazitula Java Applets 0 02-17-2008 11:45 PM
how to use btree to store data fred New To Java 1 08-07-2007 07:52 PM


All times are GMT +3. The time now is 02:19 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org