43

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.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
  • Have goolged. And YouTubed. And searched here. By everything I can see, it should be working. The only thing that I found that could be the problem is that it is all contained in a try/catch. But I'm not sure why that matters. – Christopher Robinson Apr 28 '13 at 17:43
  • The code you just posted is working fine for me. Do you get an exception or any error message? – Matthias Herlitzius Apr 28 '13 at 17:48
  • It compiles, but when I run my test main the cursor blinks and the program ends. Nothing happens, even though the code has a bunch of printlns in it. I guess my question is how to call data from a BufferedReader object within a FileReadExample object created in a separate test main. – Christopher Robinson Apr 28 '13 at 17:57
  • Just sorted your formatting out. The easiest way I find with java => stackoverflow is to format it all properly in your favourite editor first then copy paste the code into a new file, select all lines and hit tab/indent to get it recognised as code when you post here. Then paste in from that. – JonnyRaa Jul 30 '14 at 08:14

2 Answers2

81

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();
    }
}
Matthias Herlitzius
  • 3,365
  • 2
  • 20
  • 20
0

As far as i understand fr is the object of your FileReadExample class. So it is obvious it will not have any method like fr.readLine() if you dont create one yourself.

secondly, i think a correct constructor of the BufferedReader class will help you do your task.

String str;
BufferedReader buffread = new BufferedReader(new FileReader(new File("file.dat")));
str = buffread.readLine();
.
.
buffread.close();

this should help you.

  • That makes sense. So the FileReader creates it's own Buffered Reader object called 'in'. So I have an object in my testMain called fr, that should have within it a BufferedReader object that should be accessible. From the test main, how do I call that? fr.in.readLine() doesn't work. The code that you've provided here should go in FileReader or the main? Thanks, and again, sorry if I am missing something obvious. – Christopher Robinson Apr 28 '13 at 17:55
  • how about you create a method inside FileReadExample class which uses the in.readLine() method and returns a String value. So eg. if you create a method called String readALine() which returns str. you can use it in your testMain class like this-> mystr = fr.readALine(); – Onkar Borgaonkar Apr 28 '13 at 18:10
  • Boom. That got it. Accessor method, should have thought of that. Thanks. – Christopher Robinson Apr 28 '13 at 18:24