Java InputStreamReader Read readFile(File file)

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

Description

Read an entire file into a string.

License

Open Source License

Parameter

Parameter Description
file The file

Return

The contents of the file as a String.

Declaration

static public final String readFile(File file) throws IOException 

Method Source Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.File;

import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.Reader;

public class Main {
    /**/*w w w.j  a  v a 2  s. com*/
     * <p>Read an entire file into a string.</p>
     *
     * @param   reader      The file reader
     * @return         The contents of the file as a <code>String</code>.
     * @exception         If an IO error occurs.
     */
    static public final String readFile(Reader reader) throws IOException {
        StringBuffer strbuf = new StringBuffer();
        char[] charbuf = new char[1024];
        int count;
        while ((count = reader.read(charbuf)) > 0) {
            strbuf.append(charbuf, 0, count);
        }
        return strbuf.toString();
    }

    /**
     * <p>Read an entire file into a string.</p>
     *
     * @param   stream      The input stream (e.g., from <code>class.getResourceAsStream()</code>)
     * @return         The contents of the file as a <code>String</code>.
     * @exception         If an IO error occurs.
     */
    static public final String readFile(InputStream stream) throws IOException {
        Reader reader = new BufferedReader(new InputStreamReader(stream));
        return readFile(reader);
    }

    /**
     * <p>Read an entire file into a string.</p>
     *
     * @param   file      The file
     * @return         The contents of the file as a <code>String</code>.
     * @exception         If an IO error occurs.
     */
    static public final String readFile(File file) throws IOException {
        Reader reader = new FileReader(file);
        return readFile(reader);
    }

    /**
     * <p>Read an entire file into a string.</p>
     *
     * @param   filename   The file
     * @return         The contents of the file as a <code>String</code>.
     * @exception         If an IO error occurs.
     */
    static public final String readFile(String filename) throws IOException {
        return readFile(new File(filename));
    }
}

Related

  1. readFile(File f)
  2. readFile(File file)
  3. readFile(File file)
  4. readFile(File file)
  5. readFile(File file)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File file)
  9. readFile(File file)