File file = new File("FILE YOU LIKE TO READ."); DataInputStream inputStream = null; try { byte[] bytes = new byte[(int) file.length()]; inputStream = new DataInputStream(new FileInputStream(file)); inputStream.readFully(bytes); return new String(bytes); } finally { if (inputStream != null) { inputStream.close(); } }
Of course this require you to know how large the file is that you like to read. It will not work on a arbitrary stream. Also if you are using Java7 the try/finally can be omitted, YEJ!
Inga kommentarer:
Skicka en kommentar