Here you can find the source of readTextFile(String filename)
Parameter | Description |
---|---|
filename | name of file to read from |
Parameter | Description |
---|---|
IllegalStateException | if could not read the file |
public static String readTextFile(String filename)
//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(); } }