Quantcast
Viewing all articles
Browse latest Browse all 36

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

With Java 7, this is my preferred option to read a UTF-8 file:

String content = new String(Files.readAllBytes(Paths.get(filename)), "UTF-8");

Since Java 7, the JDK has the new java.nio.file API, which provides many shortcuts, so 3rd party libraries are not always required for simple file operations.


Since people are still upvoting this answer, here is a better solution that got introduced in Java 11:

String content = Files.readString(path);

Viewing all articles
Browse latest Browse all 36

Trending Articles



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