Skip to content

Instantly share code, notes, and snippets.

@gkhays
Last active July 7, 2022 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkhays/93a5ee9577c690bc5196 to your computer and use it in GitHub Desktop.
Save gkhays/93a5ee9577c690bc5196 to your computer and use it in GitHub Desktop.
Some methods to read a file from disk into a string.

Read a File from Disk into a String

Various methods for reading the contents of a file from disk into a string.

Using NIO

One-liner bonus! 😄

String contents = new String(Files.readAllBytes(Paths.get(fileName)));

Using Java 7 Scanner

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

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:

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:

  1. Using IOUtils.toString ( Apache Utils ) String result = IOUtils. ...
  2. Using CharStreams ( guava ) String result = CharStreams. ...
  3. Using Scanner (JDK) ...
  4. Using Stream Api ( Java 8 ). ...
  5. 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.

Using Java 8

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()));

Stream and Lambda Expressions

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:

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.

Another Reference

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());
}

http://www.baeldung.com/reading-file-in-java

public class ReadFile {
public static String LINE_SEPARATOR = System.getProperty("line.separator");
public static String inputStreamToString(InputStream is, boolean appendNewLine) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
if (appendNewLine) {
sb.append(LINE_SEPARATOR);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
static InputStream loadResourceAsStream(String name) {
return Configuration.class.getResourceAsStream(name);
}
public static String readFileToString(String path) {
File file = new File(path);
FileInputStream fis = null;
String delimitedString = null;
try {
fis = new FileInputStream(file);
delimitedString = inputStreamToString(fis, true);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return delimitedString;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment