Home Java Example Java Io BufferReader example in Java

Related Tutorials


 
 

Share on Google+Share on Google+

BufferReader example in Java

Advertisement
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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 19, 2011

Related Tutorials

Discuss: BufferReader example in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
faiza arshad
October 2, 2011
functions

types of fuctions
nitesh ahirwar
June 6, 2012
need some program

if i want to take some data from input suppose i have taken 3 line from keyboard in a file so haw can i find which line has more word than print the line(which having more word)
DMCA.com