Reading a File in Java
Different ways to read a text file into a String in Java.
BufferedReader
java.io.FileReader can be used to read character files. We often see it used together with BufferedReader.
java.io.BufferedReader is the class we can use to read text from a character-input stream. It bufferes characters for efficient reading of characters, arrays and lines.
1 | public static String readFile(String path) { |
Alternatively, you can use Files.newBufferedReader
method to quickly create a BufferedReader.
1 | public static String readFile(String path) { |
Files.readAllBytes
java.nio.file.Files class provide static methods to operate on files, directories, or other types of files. We can use Files.readAllBytes
to read the file. This is simplier than BufferedReader.
1 | public static String readFile(String path) { |
readAllBytes is not intended for reading in large files.
Files.readString
java.nio.file.Files has readString(Path)
method to read all content from a file into a string.
1 | public static String readFile(String path) { |
FIles.lines
java.nio.file.Files has lines(Path)
method to read all lines from a file as a Stream.
1 | public static String readFile(String path) { |
FileUtils
To use FileUtils or IOUtils to read file, we need to add commons-io as maven dependency.
1 | <dependency> |
FileUtils.readFileToString
We can use FileUtils.readFileToString
method to read the contents of a file into a string using the specified charset.
1 | public static String readFile(String path) { |
FileUtils.readLines
We can also use FileUtils.readLines
method to read lines and then concatenate all the lines together.
1 | public static String readFile(String path) { |
IOUtils
IOUtils.toString
This is similar to FileUtils.readFileToString except IOUtils takes an InputStream class. When we have a InputStream, It is more convenient to use IOUtils instead.
1 | public static String readFile(String path) { |
IOUtils.readLines
This is similar to FileUtils.readLines except IOUtils takes an InputStream class.
1 | public static String readFile(String path) { |
Read File from Classpath
Both Class and ClassLoader provides methods to get resource from classpath.
Class.getResource
can take a “relative” resource name, which is treated relative to the class’s package. Alternatively you can specify an “absolute” resource name by using a leading slash. Classloader
resource paths are always deemed to be absolute.
We can get the classloader of a class and then use getResourceAsStream method to get resource as InputStream. After we get InputStream, we can use InputStream.readAllBytes
method to convert InputStream to bytes and create String from the bytes.
Example to read file in src/main/resources/com/xinghua24/file.txt assuming the class is in com.xinghua24 package.
1 | public String readFileFromClasspath() { |
Example to read file in src/main/resources/com/xinghua24/file.txt assuming the class is in com.xinghua24 package using getClass().getResourceAsStream() mehtod.
1 | public String readFileFromClasspath() { |
The difference between getClass().getResource()
and getClass().getClassLoader().getResource()
:
- For ClassLoader, all paths are “absolute” already. There is no context from which they could be relative. Therefore you don’t need a leading slash.
- For Class, all paths are “relative” by default. the path is relative to the package of the class unless you include aleading slash(/). If path begines with a slash(/), the path becomes absolute.
ClassPathResource
If you are using Spring Framework, you can use Spring’s ClassPathResource to get the resource from classpath. through the Resource interface you can access the resource as InputStream, URL, URI or File.
1 | public String readFileFromClasspath() { |