Here you can find the source of getFileContents(final File f)
public static String getFileContents(final File f) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.IOException; import java.util.NoSuchElementException; import java.util.Scanner; public class Main { private static final String DEFAULT_ENCODING = "UTF-8"; public static String getFileContents(final File f) throws IOException { return getFileContents(f, DEFAULT_ENCODING); }// ww w . ja v a 2s . co m public static String getFileContents(final File f, final String enc) throws IOException { String content; try (Scanner scanner = new Scanner(f, enc).useDelimiter("\\Z")) { content = getScannerContents(scanner); } catch (NoSuchElementException | IllegalStateException e) { throw e; } return content; } public static String getScannerContents(final Scanner scanner) throws IllegalArgumentException { String content; if (scanner.hasNext()) { // will be false if file size == 0 content = scanner.next(); } else { content = ""; } return content; } }