Skip to content

Instantly share code, notes, and snippets.

@NickersF
Created November 6, 2016 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickersF/12fa1b8fca02451da3b2e2d25d22290e to your computer and use it in GitHub Desktop.
Save NickersF/12fa1b8fca02451da3b2e2d25d22290e to your computer and use it in GitHub Desktop.
Getting a basic buffered reader up and running
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* Created by nickersf on 11/2/2016.
*
* A class for testing the buffered reader object in Java
*/
public class SampleBufferedReader {
private BufferedReader inputStream; // BufferedReader object to read file contents
// Constructor instantiates a new BufferedReader for use
public SampleBufferedReader(String dataFilename) {
try {
inputStream = new BufferedReader(new FileReader(dataFilename));
}
catch (FileNotFoundException e) {
System.out.println("Exception thrown: " + e);
}
}
// Prints all the data in the file line by line
public void printDataFile() {
try {
while (inputStream.readLine() != null){
System.out.println(inputStream.readLine());
}
// Close the stream object
inputStream.close();
}
catch (IOException e1) {
System.out.println("Exception thrown: " + e1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment