Here you can find the source of readFile(String filename)
Parameter | Description |
---|---|
filename | Name of the file to be read. |
Parameter | Description |
---|---|
IOException | an exception |
public static String readFile(String filename) throws IOException
//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(); } }