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.InputStreamReader;

public class Main {
    /**/* w w w  . j av a  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 {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                new FileInputStream(filename)));
        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 file, String lineBreak)
  2. readFile(String fileFullPath)
  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)