Here you can find the source of readFile(final File file)
Parameter | Description |
---|---|
file | the file |
Parameter | Description |
---|---|
IOException | an exception |
protected static String readFile(final File file) throws IOException
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Main { /**//from ww w .j a va 2 s. c om * Reads a file's contents. * * @param file the file * @return the contents, as a String * @throws IOException */ protected static String readFile(final File file) throws IOException { return readStream(new FileInputStream(file)); } /** * Read a stream and return what we got as a String * * @param in the input stream * @return the contents, as a String * @throws IOException */ protected static String readStream(final InputStream in) throws IOException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final byte[] buffer = new byte[16384]; for (;;) { int count = in.read(buffer); if (count < 0) break; out.write(buffer, 0, count); } in.close(); out.close(); return out.toString(); } }