How to use BufferedReader in Java

Christopher Robinson picture Christopher Robinson · Apr 28, 2013 · Viewed 374.6k times · Source

Sorry if this is an obvious question, but I can't seem to get it. I'm working on an assignment for a Data Structures course. It involves pulling data from a simple .dat file. We had never used any of the file-accessing options in Java before, so the professor just gave us the working code for that piece.

A class called FileReadExample creates a new BufferedReader object, opens a file, and then is supposed to kick out a bunch of data about that file. But I cannot access any of the data at all.

In a separate testMain file, I created a new FileReadExample object named fr and then attempted to print out things like fr.readLine() from there, but it tells me there is no such method.

I'm sure I'm missing something staggeringly easy.

The professor's code:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileReadExample
{
    public static void main(String[] args)
    {
        System.out.println("got here");
        try
        {
            BufferedReader in = new BufferedReader(new FileReader(new File("sample-file.dat")));
            System.out.println("File open successful!");

            int line = 0;
            for (String x = in.readLine(); x != null; x = in.readLine())
            {
                line++;
                System.out.println(x);
                if (line <= 3)
                {
                    String[] tokens = x.split(" ");
                    System.out.println("Number of tokens in line " + line + ": " + tokens.length);
                    System.out.println("The tokens are:");
                    for (String token : tokens)
                    {
                        System.out.println(token);
                    }
                }
                else
                {
                    String[] tokens = x.split("\\|");
                    System.out.println("Number of tokens in line " + line + ": " + tokens.length);
                    System.out.println("The tokens are:");
                    for (String token : tokens)
                    {
                        System.out.println(token);
                    }
                    Integer[] values = new Integer[tokens.length];
                    Integer sum = 0;
                    for (int i = 0; i < tokens.length; i++)
                    {
                        sum += Integer.parseInt(tokens[i]);
                    }
                    System.out.println("Sum: " + sum);
                }
            }
        } catch (IOException e)
        {
            System.out.println("File I/O error!");
        }
    }
}

Thanks.

Answer

Matthias Herlitzius picture Matthias Herlitzius · Apr 28, 2013

Try this to read a file:

BufferedReader reader = null;

try {
    File file = new File("sample-file.dat");
    reader = new BufferedReader(new FileReader(file));

    String line;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }

} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}