How to read from file...

Hi...I'm a new comer in Java Programming.I found truble in reading a file.Here is :I want to read the file and safe the data in a String. I confuse what I must to do.So I really need help for this. For your help I say thank you.
[285 byte] By [sekarputih] at [2007-9-26 23:35:21]
# 1
Theres a good tutorial on I/O in Java here http://java.sun.com/docs/books/tutorial/essential/io/index.htmlCol
colbell at 2007-7-4 13:19:21 > top of Java-index,Archived Forums,Java Programming...
# 2
Thanks for the advice.I have read it. But for now, I need spesific code for read the file content and place it in a string.So, that tutorial it's not more convenient for me.May be you can give the part of code that implement the task above.Thank you.
sekarputih at 2007-7-4 13:19:21 > top of Java-index,Archived Forums,Java Programming...
# 3
use the following code.don`t forget to import java.io.* BufferedReader br=new BufferedReader(new InputStreamReader(System.in));String str=br.readLine(); ps:the above code can throw an IOException.
shodhansheth at 2007-7-4 13:19:21 > top of Java-index,Archived Forums,Java Programming...
# 4
Wait...I think this is code used in console program, isn't it ?, when we want to read input from keyboard.But if I use it to read a file content, so, where I must give the "file name argument" ?
sekarputih at 2007-7-4 13:19:21 > top of Java-index,Archived Forums,Java Programming...
# 5

try

{

BufferedReader br = new BufferedReader(new FileReader("MyFile.txt"));

String line = br.readLine();

}

catch(FileNotFoundException fnfe)

{

System.out.println("File MyFile.txt not found.");

}

catch(IOException ioe)

{

System.out.println("Unable to read from MyFile.txt");

}

hande123 at 2007-7-4 13:19:21 > top of Java-index,Archived Forums,Java Programming...
# 6

The code previously suggested will only get you the first line. Try this instread:

try

{

BufferedReader br = new BufferedReader(new FileReader("MyFile.txt"));

StringBuffer line = new StringBuffer();

String temp = null;

while( ( temp = br.readLine() ) != null )

{

line.append( temp );

}

// Then use line.toString() to convert to a String.

}

catch(FileNotFoundException fnfe)

{

System.out.println("File MyFile.txt not found.");

}

catch(IOException ioe)

{

System.out.println("Unable to read from MyFile.txt");

}

pzampino at 2007-7-4 13:19:21 > top of Java-index,Archived Forums,Java Programming...