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.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(); } }