1

I know how to read in lines with Scanner, but how do I use a BufferedReader? I want to be able to read lines into an array. I am able to use the hasNext() function with a Scanner but not a BufferedReader, that is the only thing I don't know how to do. How do I check when the end of the file text has been reached?

BufferedReader reader = new BufferedReader(new FileReader("weblog.txt"));

String[] fileRead = new String[2990];
int count = 0;

while (fileRead[count] != null) {
    fileRead[count] = reader.readLine();
    count++;
}
4
  • Could you add the relevant tags to the post? (Java, for example) :) Commented Jul 28, 2015 at 21:57
  • Hint : tutorialspoint.com/java/io/bufferedreader_readline.htm Commented Jul 28, 2015 at 22:07
  • Here is a post on that: stackoverflow.com/questions/22840689/… Commented Jul 28, 2015 at 22:07
  • If you like to stay in the programmer business, then you should start to learn to do research on your own. There a hundrets of examples and tutorials about reading files with a BufferedReader and you haven't found one of them? Commented Jul 28, 2015 at 22:12

3 Answers 3

1

readLine() returns null after reaching EOF.

Just

do {
  fileRead[count] = reader.readLine();
  count++;
} while (fileRead[count-1]) != null);

Of course this piece of code is not the recommended way of reading the file, but shows how it might be done if you want to do it exactly the way you attempted to ( some predefined size array, counter etc. )

Sign up to request clarification or add additional context in comments.

4 Comments

The correct way would be to use some kind of list correct? I'm just getting into i/o and haven't any concrete way of learning the rite methods on text/binary io. For this example I just wanted to see this particular way of storing was possible. Thanks!
@HugoPerea Yeah, lists are very flexible and are perfect for storing unknown amount of objects. They'd also reduce need of counter, as you can check number of objects in the list with its size() method.
@HugoPerea "The correct way would be to use some kind of list correct?" And to avoid adding stuff to your collection that is not part of the read text file, like this code currently does.
Whats the best kind of combination to read a a large text file? My teacher uses a scanner and FileInputStream.
1

The documentation states that readLine() returns null if the end of the stream is reached.

The usual idiom is to update the variable that holds the current line in the while condition and check if it's not null:

String currentLine;
while((currentLine = reader.readLine()) != null) {
   //do something with line
}

As an aside, you might not know in advance the number of lines you will read, so I suggest you use a list instead of an array.

If you plan to read all the file's content, you can use Files.readAllLines instead:

//or whatever the file is encoded with
List<String> list = Files.readAllLines(Paths.get("weblog.txt"), StandardCharsets.UTF_8);

Comments

1

using readLine(), try-with-resources and Vector

    try (BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\weblog.txt")))
    {
        String line;
        Vector<String> fileRead = new Vector<String>();

        while ((line = bufferedReader.readLine()) != null) {
            fileRead.add(line);
        }

    } catch (IOException exception) {
        exception.printStackTrace();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.