Quantcast
Channel: How do I create a Java string from the contents of a file? - Stack Overflow
Viewing all articles
Browse latest Browse all 36

Answer by DaWilli for How do I create a Java string from the contents of a file?

$
0
0

If you're willing to use an external library, check out Apache Commons IO (200KB JAR). It contains an org.apache.commons.io.FileUtils.readFileToString() method that allows you to read an entire File into a String with one line of code.

Example:

import java.io.*;import java.nio.charset.*;import org.apache.commons.io.*;public String readFile() throws IOException {    File file = new File("data.txt");    return FileUtils.readFileToString(file, StandardCharsets.UTF_8);}

Viewing all articles
Browse latest Browse all 36

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>