File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting String input from file

 
Tokai Moshai
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Tokai Moshai
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind, its BufferedReader. I got it to work. Thanks mate.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic