Here you can find the source of readFile(File file)
Parameter | Description |
---|---|
file | The file this function reads from. |
Parameter | Description |
---|---|
IOException | If the file cannot be read or something similar |
public static String readFile(File file) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.*; public class Main { /**/*from w w w. j a v a 2 s .c om*/ * Reads the contents of a file * * @param file The file this function reads from. * @return The contents or null on error. * @throws IOException If the file cannot be read or something similar */ public static String readFile(File file) throws IOException { if (file.exists()) { if (file.canRead()) { StringBuilder content = new StringBuilder(); BufferedReader input = null; try { input = new BufferedReader(new FileReader(file)); char[] buffer = new char[1024]; int n; while ((n = input.read(buffer)) != -1) { content.append(buffer, 0, n); } } finally { if (input != null) { input.close(); } } return content.toString(); } else { throw new IOException("File " + file.getPath() + " is not readable."); } } else { throw new FileNotFoundException("File " + file.getPath() + " does not exist."); } } }