Here you can find the source of readTextFile(String path)
Parameter | Description |
---|---|
path | File to read |
Parameter | Description |
---|---|
IOException | in case of failure |
public static String readTextFile(String path) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Main { /**// w w w .j ava2s .c o m * Reads the contents of a text file into a String. * <p> * Notes: - Assumes file uses the system default encoding - Newlines are converted to * the system default * * @param path File to read * @return File contents * @throws IOException in case of failure */ public static String readTextFile(String path) throws IOException { StringBuilder contents = new StringBuilder(); String lineSep = System.getProperty("line.separator"); BufferedReader input = new BufferedReader(new FileReader(path)); try { String line; while ((line = input.readLine()) != null) { contents.append(line); contents.append(lineSep); } } finally { input.close(); } return contents.toString(); } }