Java – Read File as String
To read File as String in Java, you can use BufferedInputStream, FileUtils of commons.io, etc. There are many ways to solve this problem of reading file to a string.
You can read file line by line, or fixed lengths of byte array in a while loop, or as a whole string based on the requirements of your application.
In this tutorial, we will learn some of the ways to read file as a string.
1. Read File as String using BufferedInputStream
In this example, we will use BufferedInputStream as the main step to read the contents of a file to a string. Following is the sequence of steps.
- Create file object with the path of the text file.
- Create a FileInputStream with the file created in the above step.
- Using this FileInputStream, create a BufferedInputStream.
- Use BufferedInputStream.readAllBytes() to read all the bytes to a byte array.
- Create a String with the byte array passed as argument, so that it returns a String formed using the byte array.
- Close BufferedInputStream and FileInputStream to release any system resources associated with the streams.
ReadFileAsString.java
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; /** * Java Example Program to Read File as Sting using BufferedInputStream */ public class ReadFileAsString { public static void main(String[] args) { File file = new File("files/data.txt"); try (FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis)) { //read all bytes from buffered input stream and create string out of it String fileContents = new String(bis.readAllBytes()); System.out.print(fileContents); bis.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } }
FileInputStream and BufferedInputStream could throw IOException. Hence we used Java Try with Resource.
Run the program from command prompt or in your favorite IDE.
Output
Hello reader! Welcome to www.tutorialkart.com. Hi reader! Welcome to Java Tutorials.
2. Read File as String using commons.io
In this example, we shall use apache’s commons.io package to read file as a string.
- Create file object with the path to the text file.
- Call the method FileUtils.readFileToString() and pass the file object as argument to it. The function returns data in file as String.
ReadFileAsString.java
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; /** * Java Example Program to Read File as Sting using commons.io */ public class ReadFileAsString { public static void main(String[] args) { File file = new File("files/data.txt"); String fileContents = ""; try { //read file as string fileContents = FileUtils.readFileToString(file); } catch (IOException e) { e.printStackTrace(); } //print contents of file System.out.print(fileContents); } }
Run the program and you get the following output.
Output
Hello reader! Welcome to www.tutorialkart.com. Hi reader! Welcome to Java Tutorials.
Conclusion
In this Java Tutorial, we learned how to read File as String, using inbuilt classes and some external Java packages.