How to Read a File to String in Java
In this tutorial, we will learn how to read a file to String in Java. Storing data into a file is useful for storing permanently. Generally, we use files with extensions *.txt
these files are also known as plain text files. Although a file can be of any PDF, DOC or SQL, etc. In Java, to read files mainly there are four classes. These classes belong to java.io
package and are used for file handling in Java.
In this tutorial, we will use this sampledata.txt
file to perform the read operation and this data will be converted into a String. The file data is shown below.
This is the sample data stored in a file
Rahul:10025
Sohan:10026
Madan:10027
Jack:10028
All the paths of the file shown in the code sample respect to change by machine to machine. Please verify the path of the sample file in your system and add that path to the program correctly.
Example of Retrieving String from a file using FileReader class
In the example below, we are using FileReader
class to read data from the file. An object of FileReader
class points to the beginning of a file and we will print everything while it returns -1 because it indicates the end of the file and the data is appended to a String variable.
import java.io.*;
public class StudyTonight
{
public static void main(String[] args) throws Exception
{
FileReader fr = new FileReader("E:\\Studytonight\\sampledata.txt");
String fileData = "";
int c;
while((c=fr.read()) != -1)
fileData += (char)c;
System.out.println(fileData);
}
}
This is the sample data stored in a file Rahul:10025 Sohan:10026 Madan:10027 Jack:10028
Example of Retrieving String from a file using Scanner class
Scanner class takes input from a particular source. Most of the time we provide System.in
as a source of input for giving input from a keyboard. In our program, we are going to read data from a text file so we will provide the file as an input source.
Do not forget to add useDelimiter("\\Z")
method to delimited the reading from the file.
import java.io.*;
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args) throws Exception
{
File file = new File("E:\\Studytonight\\sampledata.txt");
//Scanner will read all the data from source file
Scanner sc = new Scanner(file);
//Delimiter reads upto the end of input
sc.useDelimiter("\\Z");
System.out.println(sc.next());
}
}
This is the sample data stored in a file Rahul:10025 Sohan:10026 Madan:10027 Jack:10028
Example of Retrieving String from a file using FileInputStream class
In this example, we will use a FileInputStream
class to read data from a file then we will create a buffer of 10-byte size to store temporary bytes after reading from the file. These buffers will be appended to StringBuilder
and later we will convert that data to String using the toString()
method.
import java.io.*;
public class StudyTonight
{
public static void main(String[] args) throws Exception
{
File file = new File("E:\\Studytonight\\sampledata.txt");
FileInputStream fileInputStream = new FileInputStream(file);
byte[] buffer = new byte[10];
StringBuilder sb = new StringBuilder();
while (fileInputStream.read(buffer) != -1)
{
sb.append(new String(buffer));
buffer = new byte[10];
}
fileInputStream.close();
String fileData = sb.toString();
System.out.println(fileData);
}
}
This is the sample data stored in a file Rahul:10025 Sohan:10026 Madan:10027 Jack:10028
Example of Retrieving String from a file using BufferedReader class
The BufferedReader
is the most preferred class to read the data in a simple way. It will read all the data at the same time and then we will print it line by line using readLine()
method. At the end of the file readLine()
will return a null value and there we will stop reading data.
import java.io.*;
public class StudyTonight
{
public static void main(String[] args) throws Exception
{
File file = new File("E:\\Studytonight\\sampledata.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null)
System.out.println(line);
}
}
This is the sample data stored in a file Rahul:10025 Sohan:10026 Madan:10027 Jack:10028
Conclusion
In this article, we saw many ways to read data from file to string such as four ways to retrieve data from the file where we use FileReader, Scanner, FileInputStream, Using BufferedReader.
In some cases, we need to take care of delimiters otherwise it will read infinite times.