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 wau for How do I create a Java string from the contents of a file?

$
0
0

A flexible solution using IOUtils from Apache commons-io in combination with StringWriter:

Reader input = new FileReader();StringWriter output = new StringWriter();try {  IOUtils.copy(input, output);} finally {  input.close();}String fileContents = output.toString();

It works with any reader or input stream (not just with files), for example when reading from a URL.


Viewing all articles
Browse latest Browse all 36

Trending Articles



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