Here you can find the source of readFile(String path)
Parameter | Description |
---|---|
path | The file path |
public static String readFile(String path)
//package com.java2s; //License from project: Open Source License import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class Main { /**/* w w w . j av a 2 s. c om*/ * Read a text file and return the contents as a String. * * @param file The file * @return The (text) file contents */ public static String readFile(File file) { BufferedReader br = null; String out = ""; try { String currentLine; br = new BufferedReader(new FileReader(file)); while ((currentLine = br.readLine()) != null) out += currentLine + "\n"; } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); } catch (IOException ex) { ex.printStackTrace(); } } return out; } /** * Read a text file from the given path and return the contents as a String. * * @param path The file path * @return The (text) file contents */ public static String readFile(String path) { return readFile(new File(path)); } }