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.
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.
With the BufferdRreader nothing but if you loose the reference to stream and cannot close it and this leads to a memory leak.
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.
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(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With