Java InputStreamReader Read readfile(String filename)

Here you can find the source of readfile(String filename)

Description

Read the contents of a file.

License

Open Source License

Parameter

Parameter Description
filename file to read, '-'|null => stdin.

Exception

Parameter Description
IOException an exception

Return

The contents of the file as a string

Declaration


static public String readfile(String filename) throws IOException 

Method Source Code


//package com.java2s;
/*/*from www .  j av a 2  s  .c  o  m*/
This software is released under the Licence terms
described in the file LICENSE.txt.
*/

import java.io.*;

public class Main {
    /**
     * Read the contents of a file.
     *
     * @param filename file to read, '-'|null => stdin.
     * @return The contents of the file as a string
     * @throws IOException
     */

    static public String readfile(String filename) throws IOException {
        InputStream input = null;
        if (filename == null)
            input = System.in;
        else
            input = new FileInputStream(filename);
        InputStreamReader rdr = new InputStreamReader(input, "UTF-8");
        int c = 0;
        StringBuilder buf = new StringBuilder();
        while ((c = rdr.read()) >= 0) {
            buf.append((char) c);
        }
        input.close();
        return buf.toString();
    }
}

Related

  1. readFile(String fileName)
  2. readFile(String fileName)
  3. readFile(String fileName)
  4. readFile(String fileName)
  5. readFile(String fileName)
  6. readFile(String filename, boolean addEOL)
  7. readFile(String fileName, Class clazz)
  8. readFile(String fileName, String characterEncoding)
  9. readFile(String fileName, String characterEncoding)