Here you can find the source of readFile(String path)
Parameter | Description |
---|---|
IOException | an exception |
public static String readFile(String path) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class Main { /**/*from ww w . j a v a2 s . c o m*/ * Returns the content of the given file. * @throws IOException */ public static String readFile(String path) throws IOException { InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); if (is == null) { throw new FileNotFoundException("Can't find \"" + path + "\"."); } BufferedReader br = null; StringBuilder sb = new StringBuilder(); try { String line; br = new BufferedReader(new InputStreamReader(is)); while ((line = br.readLine()) != null) { sb.append(line + "\n"); } } finally { if (br != null) { try { br.close(); } catch (IOException e) { throw new RuntimeException(e); } } } return sb.toString(); } }