Learn how to use BufferReader class in Java.
Read File Line by line using BufferedReader
Read File Line by line using BufferedReader
In this section you will learn how to read line by line data from a file using BufferedReader.
In this section, we provide you two examples:
1. Read line by only using BufferedReader
2. Read line by using FileInputStream, DataInputStream, BufferedReader.
What is BufferedReader ?
BufferedReader buffer character to read characters, arrays and lines. It read text from a character-input stream.
You can change the buffer size or can use default size.
What is DataInputStream ?
From reading java data types from a input stream in a machine independent way, we incorporate DataInputStream.
Example
Read line by only using BufferedReader
import java.io.*; public class BufferedReaderDemo { public static void main(String[] args) { try { BufferedReader br = new BufferedReader( new FileReader("DevFile.txt")); String devstr; while ((devstr = br.readLine()) != null) { System.out.println(devstr); } } catch (IOException e) { } } }
This will produce the following Output :
Welcome To Devmanuals Here you find the good learning stuff. As well complete coverage on each topics like java, PHP etc. |
Video: How to read file with DataInputStream and BufferedReader?
Read line by using FileInputStream, DataInputStream, BufferedReader
import java.io.*; public class BufferedReaderStream{ public static void main(String args[]) { try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("DevFile.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console System.out.println (strLine); } //Close the input stream in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } }
This will produce the following output :
Welcome To Devmanuals Here you find the good learning stuff. As well complete coverage on each topics like java, PHP etc. |