Home Java Javafile Java read text file
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java read text file

Advertisement
Here are two ways to read a text file line by line. One uses BufferReader class while other uses Scanner class.

In the section we are discussing about how to read file line by line. Here we have used FileInputStream, DataInputStream and BufferedReader classes to read a text file one line at a time. It can also be used to read large text files.

readLine() method of BufferedReader class reads complete line and return string line. When it returns null, it means the files reading is complete.

Following Java classes and Interfaces are used:

DataInputStream Class:

DataInputStream class reads primitive data from any input stream and output stream written by other program. Here we have used DataInputStream class to read the text file one line at a time. As it is not thread safe. Code must be written to make the program thread safe in a multi threaded environment.

BufferedReader class

BufferedReader class reads data i.e. characters, arrays and lines from the character input stream. We have created the object of BufferedReader class by passing the the DataInputStream as constructor parameter and used it to read the data.

Example of Read text File Line by Line:

import java.io.*;
class FileRead
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("textfile.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());
}
}
}

The other way to Read file line by line is by using Scanner Class. The benefit of using Scanner class is that we do not need to convert a string to integer, it directly reads the number from the file. Here we have used hasNextLine() method to read the contents of a file line by line.

Scanner class uses nextInt(), nextLong(), wtc.

Example:

package ReadFile;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class ReadFileLineByLine {
public static void main(String[] args) throws IOException {

FileInputStream stream = new FileInputStream("c://ReadFileLineByLineInJava.txt");

DataInputStream inputStream = new DataInputStream(stream);

Scanner scanner = new Scanner(inputStream);

while (scanner.hasNextLine()) {

System.out.println(scanner.nextLine());

}

inputStream.close();

}
}

Resource:

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: October 10, 2013

Related Tutorials

Ask Questions?    Discuss: Java read text file  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com