Results 1 to 7 of 7
Thread: Help with File reading
- 05-05-2011, 08:03 PM #1
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
Help with File reading
Hi friends,
I have a file with mixture of strings and integers like this
I know exactly where the string or an integer is in the file.Java Code:aaaa 1 bbbb 2 cccc dddd 3 eeee 4
So I need to do something like this. I need to read the string "aaaa" first, then I will read 1, then I'll read "bbbb", then 2 and so on.
Is there any way in which I can do the above. Something like readNextString() or readNextInteger().
I know I can use BufferedReader and read lines and do manipulation on each lines. But I am looking for something that we have in C++ like shown below
Java Code:int number; char name[10] ; ifstream in("test.txt"); if (in.is_open()) { in >> name; [I][COLOR="YellowGreen"]//gets "aaaa" here[/COLOR][/I] in >> number; [COLOR="YellowGreen"][I]//gets 1 here[/I][/COLOR] [I][COLOR="YellowGreen"]//and so on[/COLOR][/I] }
- 05-05-2011, 08:07 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Take a look at the Scanner class
- 05-05-2011, 08:23 PM #3
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
Oh... how could I forget Scanner. Thanks for reminding. :)
But, Scanner doesn't have a getString() method nah. So any way to do it?
- 05-05-2011, 10:02 PM #4
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Try using a BufferedReader and the String class.
And use the String class's methods to work with the String you read and grab certain values from it.Java Code:File file = new File("path"); BufferedReader bReader = new BufferedReader(file); String line = bReader.readLine();
Perhaps the most useful methods for you would be substring() and indexOf().
Good luck!
- 05-05-2011, 10:50 PM #5
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
@Solarsonic : As said in my first post, I already use BufferedReader. My purpose is to extract a string and an integer separately.
I'll put it in simple way here. In one fine I have just one line which is a string-integer pair. I have a String variable and int variable in my code. From the string-integer pair, I need to put the string value in String variable and the integer value in the int variable. The programmer has the information that the string-integer pair will be separated by space.
-
As already mentioned if you were using a Scanner, you could use nextInt() to get your number and next() to get a String token and nextLine() to get a String line, but you would need to take care to handle the end of line token. I'm not sure why you dismissed this suggestion out of hand. Otherwise you could grab your lines and split them via String's split method and iterate through the array of String/tokens that are returned.
- 05-05-2011, 11:07 PM #7
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
Similar Threads
-
Reading file external to jar file
By nn12 in forum New To JavaReplies: 6Last Post: 02-04-2011, 05:46 AM -
reading a file and writing to a file....help!!!!
By java_prgr in forum New To JavaReplies: 3Last Post: 07-26-2010, 06:53 PM -
Reading and Writing the contents of a file to another file
By priyankatxs in forum New To JavaReplies: 9Last Post: 10-20-2009, 10:52 AM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM -
Reading from file
By kiab3000 in forum New To JavaReplies: 0Last Post: 03-14-2009, 06:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks