Here you can find the source of readFile(final String name)
Parameter | Description |
---|---|
name | the name of the file to read |
public static String readFile(final String name)
//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 { /**// ww w.j a v a2 s . co m * Reads the contents of a file. * * @param name * the name of the file to read * @return the contents of the file if successful, an empty string * otherwise. */ public static String readFile(final String name) { FileReader fileReader = null; final StringBuilder contents = new StringBuilder(); try { final File file = new File(name); if (!file.exists()) { throw new IllegalArgumentException("File " + name + " does not exist"); } fileReader = new FileReader(file); final BufferedReader reader = new BufferedReader(fileReader); String inputLine = reader.readLine(); while (inputLine != null) { contents.append(inputLine).append("\n"); inputLine = reader.readLine(); } reader.close(); } catch (final IOException i1) { throw new RuntimeException(i1); } return contents.toString(); } }