Java InputStreamReader Read readFile(String filename)

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

Description

Reads the given filename into a string.

License

Open Source License

Parameter

Parameter Description
filename Name of the file to be read.

Exception

Parameter Description
IOException an exception

Return

Returns a string representing the file contents.

Declaration

public static String readFile(String filename) throws IOException 

Method Source Code

//package com.java2s;

import java.io.BufferedReader;

import java.io.FileInputStream;

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

public class Main {
    /**//from www.  j  ava 2 s. co m
     * Reads the given filename into a string.
     * 
     * @param filename
     *            Name of the file to be read.
     * @return Returns a string representing the file contents.
     * @throws IOException
     */
    public static String readFile(String filename) throws IOException {
        return readInputStream(new FileInputStream(filename));
    }

    /**
     * Reads the given filename into a string.
     * 
     * @param filename
     *            Name of the file to be read.
     * @return Returns a string representing the file contents.
     * @throws IOException
     */
    public static String readInputStream(InputStream stream)
            throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                stream));
        StringBuffer result = new StringBuffer();
        String tmp = reader.readLine();

        while (tmp != null) {
            result.append(tmp + "\n");
            tmp = reader.readLine();
        }

        reader.close();

        return result.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)
  7. readFile(String fileName)
  8. readFile(String fileName)
  9. readFile(String fileName)