Results 1 to 10 of 11
Greetings,
I'm kind of rusty with my java and I need some help with reading in values from a file. The file being read from is in the format:
Code:
...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 09-07-2005 #1Just Joined!
- Join Date
- Feb 2005
- Location
- At a rock house in Oak Cliff...
- Posts
- 87
Java File I/O
Greetings,
I'm kind of rusty with my java and I need some help with reading in values from a file. The file being read from is in the format:
for each line. I'm lost on how to set this up and any help is always appreciated.Code:string string int string string int
- 09-07-2005 #2Basically, we wrap the BufferedReader around the FileReader. FileReader takes either a String with the file's location or an instance of the File class.Code:
import java.io.*; BufferedReader bfrRead = new BufferedReader(new FileReader("/home/user/temp/the_file")); try { while(bfrRead.open()) { String theLine = bfrRead.readLine(); // Do Stuff } bfrRead.close(); } // Now catch the FileNotFoundException and IOException
Then, while lines are still there, we read them in, line by line. After all lines are read, we close that BufferedReader.
Make sure to catch your Exceptions. I'm 99% sure that only the two I named need to be caught.
And, of course, be sure to import java.io.*;
The 1.4.2 API is at:
http://java.sun.com/j2se/1.4.2/docs/api/
If it helps
- 09-07-2005 #3
You might also want to use the java.util.StringTokenizer class to seperate the tokens on a line. (I'd recommend reading the API for it; it's quite useful)
- 09-07-2005 #4
Javasnob has a good idea.
Definitely check out the API. But as a brief overview:
StringTokenizer takes a String and separates it by whitespace into "Tokens". You can then manipulate the Tokens separately:
Code:String theString = new String("a b c d e f"); StringTokenizer st = new StringTokenizer(theString); while(st.hasTokens()) { String theToken = st.nextToken(); System.out.println(theToken); }
Unrelated to that, but as I just remembered, remember that I/O is done as Strings. So even though you have a string string int string string int format, you will receive them all as Strings.
To change the Strings to ints, you'll need to do:
That can throw a NumberFormatException however, be warned.Code:int theNum = Integer.parseInt(theStringWithIntValue);
- 09-08-2005 #5Just Joined!
- Join Date
- Feb 2005
- Location
- At a rock house in Oak Cliff...
- Posts
- 87
Thank you for all your help. I've implemented your suggestions into my code and all is well with the exception of one: for
I get the error:Code:while(bfrRead.open())
Code:The method open() is undefined for the tyoe BufferedReader
- 09-09-2005 #6
Gadzooks!
It's actually
Oopsie.Code:while(bfrRead.ready())
- 09-10-2005 #7
I always liked doing this with BufferedReader:
But that's just another suggestion to cut down on lines of code.Code:BufferedReader reader = ...; String line; while((line = reader.readLine()) != null) { ... }
- 09-10-2005 #8Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
I thought Java was supposed to be a good language. It certainly makes a meal of doing something that's trivial in a proper language like C.
- 09-10-2005 #9
J2SE 5.0 makes this easier, actually:
Code:Scanner in = new Scanner(new FileInputStream(file)); while(in.hasNext()) { String str = in.next(); // Process the string }
- 09-13-2005 #10Linux Newbie
- Join Date
- Jul 2005
- Location
- Chd,India
- Posts
- 135
how about using perl regular expressions?
The strong shall live and the weak will die
In the end,only the fittest survive in this world
- Shishio Makoto


Reply With Quote
