Java FileInputStream Read readFile(final File file)

Here you can find the source of readFile(final File file)

Description

Reads a file's contents.

License

Open Source License

Parameter

Parameter Description
file the file

Exception

Parameter Description
IOException an exception

Return

the contents, as a String

Declaration

protected static String readFile(final File file) throws IOException 

Method Source Code


//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();
    }
}

Related

  1. readFile(File srcFile, OutputStream destStream)
  2. readFile(File target)
  3. readFile(FileInputStream fileinputstream, byte abyte0[])
  4. readFile(final File file)
  5. readFile(final File file)
  6. readFile(final InputStream stream)
  7. readFile(final String fileNamePath)
  8. readFile(InputStream ios)
  9. readFile(InputStream iStream)