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

$
0
0

This one uses the method RandomAccessFile.readFully, it seems to be available from JDK 1.0 !

public static String readFileContent(String filename, Charset charset) throws IOException {    RandomAccessFile raf = null;    try {        raf = new RandomAccessFile(filename, "r");        byte[] buffer = new byte[(int)raf.length()];        raf.readFully(buffer);        return new String(buffer, charset);    } finally {        closeStream(raf);    }} private static void closeStream(Closeable c) {    if (c != null) {        try {            c.close();        } catch (IOException ex) {            // do nothing        }    }}

Viewing all articles
Browse latest Browse all 36

Trending Articles



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