title: How to use BufferedReader in Java
body: <p>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.</p>
  
  <p>A class called <code>FileReadExample</code> creates a new <code>BufferedReader</code> 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.</p>
  
  <p>In a separate <code>testMain</code> file, I created a new <code>FileReadExample</code> object named <code>fr</code> and then attempted to print out things like <code>fr.readLine()</code> from there, but it tells me there is no such method.</p>
  
  <p>I'm sure I'm missing something staggeringly easy.</p>
  
  <p>The professor's code:</p>
  
  <pre><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 &lt;= 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 &lt; tokens.length; i++)
                      {
                          sum += Integer.parseInt(tokens[i]);
                      }
                      System.out.println("Sum: " + sum);
                  }
              }
          } catch (IOException e)
          {
              System.out.println("File I/O error!");
          }
      }
  }
  </code></pre>
  
  <p>Thanks.</p>