English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

How do I use BufferedReader to read whole paragraph? I dont know how to start it except typing:

class Reader throws IO Exception
{
BufferedReader reader - new BufferedReader();
}


I know Im supposed to use readline() but I just dont know how to put it......

BTW can Bufferedreader read strings inside a vector?

2007-11-03 19:57:38 · 3 answers · asked by Bob 3 in Computers & Internet Programming & Design

3 answers

1>Create a String object to hold the text to be read

2>Enclose code in try and catch statements for exception handling

3>Associate the file to be read with a File object

4>Wrap a FileReader object within a BufferedReader object

5>Use a while loop and the readLine method of BufferedReader class to
iterate through the text and print it to the console or in the text area.

6>Close the stream when there are no more lines of text to read

The 4th step is definitely a little odd. Why do we need that step anyway? Well, because FileReader reads just a single character at a time and hence provides a very inefficient method of reading. On the other hand BufferedReader provides methods for efficient reading of characters, arrays, and lines. But BufferedReader can't be used by itself however.
Instead, you must wrap or chain a Reader into BufferedReader. This is done by passing a Reader (Here a FileReader) object into the constructor of BufferedReader.

Now see the code below.

private void readFileIntoTextArea() {
try {
//Associate the file to be read with a File object
f = new File("anyDamnFile.txt");
//Wrap an instance of a FileReader stream object in an instance of a BufferedReader object
br = new BufferedReader (new FileReader(f));
/*
Loop through each line using a while construct with the readLine method, and assign it to the String variable str you created earlier in ur code.
*/
//opens while
while ((str = br.readLine()) != null) {
/*
Pass the text from the file into the text area with the append method from the JTextArea class. You created this variable earlier, instruct.
*/
myTxt.append(str);
}
//closes while

}
//close try

//open catch for FileNotFoundException thrown by FileReader class

catch (FileNotFoundException fnfe) {
System.out.println(fnfe);

Any further code u need..................

Hope it helps.

2007-11-03 22:51:31 · answer #1 · answered by binaryFusion 5 · 0 0

Yes you can read strings inside a vector.

2007-11-03 20:06:14 · answer #2 · answered by Anonymous · 0 0

Try these two links

2007-11-03 20:30:21 · answer #3 · answered by AnalProgrammer 7 · 0 0

fedest.com, questions and answers