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

$
0
0
public static String slurp (final File file)throws IOException {    StringBuilder result = new StringBuilder();    BufferedReader reader = new BufferedReader(new FileReader(file));    try {        char[] buf = new char[1024];        int r = 0;        while ((r = reader.read(buf)) != -1) {            result.append(buf, 0, r);        }    }    finally {        reader.close();    }    return result.toString();}

Viewing all articles
Browse latest Browse all 36

Trending Articles



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