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

$
0
0

To read a File as binary and convert at the end

public static String readFileAsString(String filePath) throws IOException {    DataInputStream dis = new DataInputStream(new FileInputStream(filePath));    try {        long len = new File(filePath).length();        if (len > Integer.MAX_VALUE) throw new IOException("File "+filePath+" too large, was "+len+" bytes.");        byte[] bytes = new byte[(int) len];        dis.readFully(bytes);        return new String(bytes, "UTF-8");    } finally {        dis.close();    }}

Viewing all articles
Browse latest Browse all 36

Trending Articles



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