In this post, we will learn about how to read a file from java with example
This will called as IO operations
There are many ways to read a file from java. BufferedReader, Scanner, Streams
BufferReader
BufferReader is one of the common way to read a file from java. It will read the file line by line
It is synchronized, so the operations will be thread safe
Once the read operation is done we need to close the BufferReader
we are getting chance of IOException in this code, so we should surround the code by try catch
Example
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class ReadFileExample {
public static void main(String[] args) {
try {
File file = new File("C://temp/sample.txt");
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
// Converting input stream to buffer reader
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String temp = "";
StringBuffer textFileContent = new StringBuffer();
while ((temp = bufferedReader.readLine()) != null) {
textFileContent.append(temp);
}
System.out.println(textFileContent);
// In below line we are closing the bufferReader
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output
sdfdsfdsfsdfsHelloworld
Scanner
In this example we will do the same operation using Scanner. Here are also we used while loop to iterate the content of file as like bufferedReader
Once we done with the operation we nee to close the scanner as like BufferReader
Scanner Example
import java.io.File;
import java.util.Scanner;
public class ReadFileScannerExample {
public static void main(String[] args) {
try {
File file = new File("C:\\temp\\sample.txt");
Scanner scanner = new Scanner(file);
StringBuffer textFileContent = new StringBuffer();
while (scanner.hasNextLine()) {
textFileContent.append(scanner.nextLine());
}
System.out.println(textFileContent);
scanner.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output
sdfdsfdsfsdfsHelloworld
Using NIO pacakge
Using NIO package we have Files.ReadAllBytes method. Using this we can read file as byte array. Then we can convert this into a string as like below example
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Sample {
public static void main(String[] args) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get("D:\\workspace\\sample.txt"));
String s = new String(encoded, StandardCharsets.US_ASCII);
System.out.println(s);
}
}
java.io.FileNotFoundException
If java not able to find the file it will throw the below exception
java.io.FileNotFoundException: C:\temp\sample.txt1 (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:611)
at com.geeks.example.ReadFileScannerExample.main(ReadFileScannerExample.java:12)
Github
Related Articles
create text file from java with example