File Reading Example

File Reading Example


Posted in : Java Posted on : March 29, 2012 at 7:11 PM Comments : [ 0 ]

In this tutorial you will learn how to read data from a file

Java File Reading Example

java.io package provides a numbers of classes to access the file. You use a combination of  IO classes to access a file. The following is an example to read a file.

FileExample.java

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class FileExample {
	public static void main(String[] args) {
		try {
			File file = new File("D:\\test-file.txt");
			FileInputStream fileInputStream = new FileInputStream(file);
			DataInputStream dataInputStream = new DataInputStream(
					fileInputStream);
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(dataInputStream));
			String fileData = "";
			while ((fileData = bufferedReader.readLine()) != null) {
				System.out.println(fileData);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


When you run this application it will display message as shown below:


Teating a Java File

Date 29 March 2012

Download Select Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics