Java FileReader Create readTextFile(String filename)

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

Description

read Text File

License

MIT License

Parameter

Parameter Description
filename name of file to read from

Exception

Parameter Description
IllegalStateException if could not read the file

Return

text within the file

Declaration

public static String readTextFile(String filename) 

Method Source Code

//package com.java2s;
/** Copyright by Barry G. Becker, 2000-2011. Licensed under MIT License: http://www.opensource.org/licenses/MIT  */

import java.io.*;

public class Main {
    /**//  www .  j  a  v  a 2s.c  om
     * @param filename name of file to read from
     * @return text within the file
     * @throws IllegalStateException if could not read the file
     */
    public static String readTextFile(String filename) {
        BufferedReader br = null;
        StringBuilder bldr = new StringBuilder(1000);

        try {
            br = new BufferedReader(new FileReader(filename));

            String sCurrentLine;
            while ((sCurrentLine = br.readLine()) != null) {
                bldr.append(sCurrentLine).append('\n');
            }

        } catch (IOException e) {
            throw new IllegalStateException("Could not read " + filename, e);
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return bldr.toString();
    }
}

Related

  1. readTextFile(InputStream in)
  2. readTextFile(String completePath)
  3. readTextFile(String file)
  4. readTextFile(String file)
  5. readTextFile(String file)
  6. readTextFile(String filename)
  7. readTextFile(String fileName)
  8. readTextFile(String filename)
  9. readTextFile(String fileName)