Java Tutorials
Java vs C# - 10 Key Differences between Java and C#
What is Java? Java was released by Sun Microsystem in 1995. It was developed by James Gosling. It is a...
Java provides several mechanisms to read from File. The most useful package that is provided for this is the java.io.Reader. This class contains the Class Java BufferedReader under package java.io.BufferedReader
BufferedReader is a Java class to reads the text from an Input stream (like a file) by buffering characters that seamlessly reads characters, arrays or lines. In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream.
It is therefore advisable to wrap BufferedReader in Java around any Reader whose read() operations may be costly, such as java FileReaders and InputStreamReaders. A typical usage would involve passing the file path to the BufferedReader in Java as follows:
objReader = new BufferedReader(new FileReader("D:\DukesDiary.txt")); //Assuming you have a text file in D drive
This basically loads your file in the objReader.Now, you will need to iterate through the contents of the file and print it.
The while loop in the below code will read the file until it has reached the end of file
while ((strCurrentLine = objReader.readLine()) != null) { System.out.println(strCurrentLine); }
strCurrentLine reads the current line and the Java readLine function objReader.readLine() returns a string. Hence, the loop will iterate until it’s not null.
Below code is a Java BufferedReader example which shows the complete implementation:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ReadFileExample { public static void main(String[] args) { BufferedReader objReader = null; try { String strCurrentLine; objReader = new BufferedReader(new FileReader("D:\\DukesDiary.txt")); while ((strCurrentLine = objReader.readLine()) != null) { System.out.println(strCurrentLine); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (objReader != null) objReader.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
Note:
The above code has some very important handlings especially in the finally block of the code.
This code will ensure that the memory management is done efficiently and the objReader.close() method is called that releases the memory.
Below is the example of Java Read Files using BufferedReader class
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ReadFileExample_jdk7 { private static final String FILENAME = "D:\\DukesDiary.txt"; public static void main(String[] args) { try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) { String strCurrentLine; while ((strCurrentLine = br.readLine()) != null) { System.out.println(strCurrentLine); } } catch (IOException e) { e.printStackTrace(); } } }
What is Java? Java was released by Sun Microsystem in 1995. It was developed by James Gosling. It is a...
Why use string "charAt" Method? The charat method returns the character at the definite index. In this...
Follow the simple steps below to compile and execute any JAVA program online using your favourite...
What is JavaScript? JavaScript is a scripting language which helps you create interactive web...
For-Each Loop is another form of for loop used to traverse the array. for-each loop reduces the...
What is compareTo() method in Java? compareTo() is used for comparing two strings...