Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use BufferedReader in Java [closed]

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.

like image 869
Christopher Robinson Avatar asked Apr 28 '13 17:04

Christopher Robinson


People also ask

Does BufferedReader need to be closed?

When you are finished reading characters from the BufferedReader you should remember to close it. Closing a BufferedReader will also close the Reader instance from which the BufferedReader is reading.

What happens if BufferedReader is not closed?

With the BufferdRreader nothing but if you loose the reference to stream and cannot close it and this leads to a memory leak.

How do I turn off buffer reader?

BufferedReader close() method in Java with ExamplesThe close() method of BufferedReader class in Java is used to close the stream and release all the system resources associated with the stream operations. Parameters: This method does not accept any parameter. Return value: This method does not return any value.


1 Answers

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();     } } 
like image 136
Matthias Herlitzius Avatar answered Sep 24 '22 19:09

Matthias Herlitzius