Various methods for reading the contents of a file from disk into a string.
One-liner bonus! 😄
String contents = new String(Files.readAllBytes(Paths.get(fileName)));
In my son's high school programming class, they are teaching them to use Scanner, e.g.
String content = new Scanner(new File("filename")).useDelimiter("\\n").next();
System.out.println(content);
Which is the approach taken in the Stack Overflow article "What is simplest way to read a file into String? [duplicate]." The following article uses a similar approach, albeit with a different delimiter; see "5 ways to convert InputStream to String in Java." Instead of reading line-by-line, you can read the entire contents of the file into a single string by using "\\A"
as the delimiter. This works because the delimiter \A
is a boundary match for the beginning of input as declared in java.util.regex.Pattern
which is why Scanner
is returing the entire String
from the InputStream
.
FileInputStream fis = new FileInputStream(fileName);
String content = new Scanner(fis, "UTF-8").useDelimiter("\\A").next();
System.out.println(content);
Using a BufferedReader
, this is how I've always done it.
String ls = System.getProperty("line.separator");
String line = null;
StringBuilder stringBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(file));
try {
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
System.out.println(stringBuilder.toString());
} finally {
reader.close();
}
"Scanner vs. BufferedReader" is a nice article discussing the differences between Scanner
and BufferedReader
. See also:
- How to create a Java String from the contents of a file?
- Reading from a text file and Storing in a String [duplicate]
- How to read file in Java – FileInputStream
There is an excellent StackOverflow article that shows each of the methods above in addition to Apache Commons IOUtils and Java Lambda expressions, below.
Ways to convert an InputStream to a String:
- Using IOUtils.toString ( Apache Utils ) String result = IOUtils. ...
- Using CharStreams ( guava ) String result = CharStreams. ...
- Using Scanner (JDK) ...
- Using Stream Api ( Java 8 ). ...
- Using parallel Stream Api ( Java 8 ).
More items...
See Read/convert an InputStream to a String.
I was impressed with the benchmarks, which are illustrated in the useful-java-links repo; See ConvertInputStreamToStringBenchmark.java.
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String lines = buffer.lines().collect(Collectors.joining());
To preserve line endings.
buffer.lines().collect(Collectors.joining(System.lineSeparator()));
Just when you learn a new trick, along comes a better one! This one depends on Lambda expressions in Java 8.
public static JSONArray fileToStream(String path, String desiredType)
throws JSONException {
JSONArray identityArray = new JSONArray();
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
reader.lines()
.filter(f -> f.startsWith(desiredType))
.forEach(record -> parseDataRecord(record))
return identityArray;
}
I learned this one from Adam Bien's weblog, "Reading InputStream into String with Java 8" and a "Java 8 Stream Tutorial," by Benjamin Winterberg. See also:
- Java 8: Streaming a String (Also by Adam Bien)
- Java 8 Explained: Using Filters, Maps, Streams and Foreach to apply Lambdas to Java Collections!
- Read all lines with BufferedReader (StackOverflow)
- Java 8 Streams filter examples
- Java 8 Lambda filter by Lists
Note: Something to keep in mind when using lambda expressions is the compiler may complain about an uncaught exception even if the surrounding method has a try/catch block. So you must include a try/catch block within the lambda expression. See "Java 8 Lambda function that throws exception?".
reader.lines()
.filter(f -> f.startsWith(prefix))
.forEach(
r -> {
try {
parseDataRecord(r);
} catch (JSONException j) {
throw new RuntimeException(j);
}
});
These guys make it seem harder than it is: "How can I throw CHECKED exceptions from inside Java 8 streams?" In contrast, Bill Bejeck offers a more reasonable approach in his article "Java 8 Functional Interfaces and Checked Exceptions.
Speaking of exception handling, 9 Best Practices to Handle Exceptions in Java has some good tips.
The JDK
NIO
added the readAllBytes
method.
@Test
public void givenFilePath_whenUsingFilesReadAllBytes_thenFileData() {
String expectedData = "Hello World from fileTest.txt!!!";
Path path = Paths.get(getClass().getClassLoader()
.getResource("fileTest.txt").toURI());
byte[] fileBytes = Files.readAllBytes(path);
String data = new String(fileBytes);
Assert.assertEquals(expectedData, data.trim());
}