Read File from specified path in Java


 

Read File from specified path in Java

This example program shows you to Read File from specified path in Java. You will learn to read a text file from a directory in Java.

This example program shows you to Read File from specified path in Java. You will learn to read a text file from a directory in Java.

How to read a file from a specified path in Java?

After learning so many examples of file handling in  Java, we will show to a simple program which reads a text file from specified path in Java. We will give complete path and file name to the program and program will read content of file. After opening the file, the program will read text data line by line and print on the console.

In this tutorial we are to teach you the steps to read a file from a disk file. For simplicity we will read the content of a text file one line at a time and print data on the console. You can run the example code given here in Eclipse or from the command prompt.

Java comes with a variety of classes and interfaces to work with files and directories. If you try to read a file from which does not exist on the path specified then it will throws file not found exception. If a file exists in the path specified then our program will read the data line by line and print on the console.

In our example program we will use the BufferedReader of Java, which gives the ability to read file one line at a time. Reading text files one line at a time is the best way to read a big text file. If you are reading a small file then you can also read complete text in memory in one go. So, the strategy of reading a file totally depends on your project requirements. Java comes with the utility classes to meet all types of reprogramming requirements.

Developers can easily modify the code given here to meet the requirement of their application.

Here is a complete example of the program that reads a text file from the disk path and print on the console.


package net.roseindia;

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

public class ReadFileFromDirectoryPath {

public static void main(String[] args) {
	String strFileDirectoryPath = "d:/1.txt";
	try {
	FileInputStream fstream = new FileInputStream(strFileDirectoryPath);
	DataInputStream in = new DataInputStream(fstream);
	BufferedReader br = new BufferedReader(new InputStreamReader(in));
	String strLine;
	// Read File Line By Line
	while ((strLine = br.readLine()) != null) {
		System.out.println(strLine);
	}
	in.close();
	} catch (Exception e) {// Catch exception if any
		System.err.println("Error: " + e.getMessage());
	}
}

}

In the above program we are using the FileInputStream, DataInputStream, InputStreamReader and BufferedReader classes to read a text file line by line. The br.readLine() method of BufferedReader is used to read one line at and time. Finally the program prints the data on console. Here is the screen shot of output of program:

You can also read the file using the Scanner class of Java. Here is the code of reading file using the Scanner class:


package net.roseindia;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadFileFromDirectoryPath {
	public static void main(String[] args) {
		String strFileDirectoryPath = "d:/1.txt";
		// Construct file object
		File file = new File(strFileDirectoryPath);
		try {
		Scanner scan = new Scanner(file);
		while (scan.hasNextLine()) {
			String scannedline = scan.nextLine();
			System.out.println(scannedline);
		}
		scan.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

}

Here is the output of the program:

The Scanner class is part of java.util package which is used for parsing primitive types and strings using regular expressions. In this example we are checking if there any one other line the help of hasNextLine() function and if line is available we are getting the line data with nextLine() function. This way the Scanner class is used for reading a text file line by line in the above example.

Related Example

Ads