This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Getting String input from file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Getting String input from file" Watch "Getting String input from file" New topic
Author

Getting String input from file

Tokai Moshai
Greenhorn

Joined: May 24, 2006
Posts: 16
OK, this is supposed to be easy, still I could not find the answer in google or searching this sub-forum. Please help.

My problem in simple form, I have a text file that has two lines:

this/is/line/1
this/is/line/2

i have two variables in my program var1 and var2.

how do I get the values of those two lines, into my two variables?
so that var1 ="this/is/line/1" and var2 = "this/is/line/2"
thanks in advanced.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
This is the most common way to read a file:

That while condition is rather complex the first time you see one like it. This part:

( line = input.readLine() )

reads one line of text into a variable called line. An assigment clause like that also has a value, which is the value of the variable just assigned. The second part of the while condition compares that to null. Null means we tried to read but we were at the end of file and got nothing.

You wanted to get two variables, line1 and line2. I didn't do that. This more general solution puts lines read into a collection or an array so it can handle any number of lines.

Did that help?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Tokai Moshai
Greenhorn

Joined: May 24, 2006
Posts: 16
Thanks for reply. I'll try it out now, and will let you know if I am able to make it to work or not.

By the way, is 'input' a FileReader type?
Tokai Moshai
Greenhorn

Joined: May 24, 2006
Posts: 16
Never mind, its BufferedReader. I got it to work. Thanks mate.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Excellent! We deliberately leave out details here ... it's much more fun if you figure them out yourself. Keep having fun!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting String input from file
 
Similar Threads
Elegant Java
"cannot find symbol variable..."
XSL + using vaiables in URLS
Which is better design
How to add two variables in XSLT