Home Java Example Java Io BufferReader example in Java



BufferReader example in Java
Posted on: February 19, 2011 at 12:00 AM
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.

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.

Download Source Code

Related Tags for BufferReader example in Java:


More Tutorials from this section

Ask Questions?    Discuss: BufferReader example in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.