Reading File Content using BufferedReader
BufferedReaderFileExample.java
package com.dineshkrish.files; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class BufferedReaderFileExample { public static void main(String[] args) throws IOException { // Defining the File Object for Input File to read File inputFile = new File("abc.txt"); // Reader Object FileReader fr = null; BufferedReader fileBF = null; try { fr = new FileReader(inputFile); fileBF = new BufferedReader(fr); // Reading the first Character int data = fileBF.read(); // Reading each character until reach last character in file while(data != -1) { System.out.print((char)data); // will return integer value(ASCII) will be converted as character data = fileBF.read(); } } catch (FileNotFoundException ex) { System.out.println(ex.getMessage()); ex.printStackTrace(); } catch (IOException ex) { System.out.println(ex.getMessage()); ex.printStackTrace(); } finally { // Closing the resources after the IO Operation fileBF.close(); fr.close(); } } }
Input File Content(ie: abc.txt)
Output
—————-
Hello everyone,
This is the sample program to read file using BufferedReader class in Java.
———
Regards,
Dinesh Krishnan
More from my site

Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.
No responses yet