Here you can find the source of readTextFileContents(File fFile)
Parameter | Description |
---|---|
fFile | The file to be read. |
Parameter | Description |
---|---|
IOException | Thrown if the input stream read failed. |
public static String readTextFileContents(File fFile) throws IOException
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.Reader; public class Main { /**/* w w w. j av a 2 s . c om*/ * Reads the text file contents and returns it as a string. * * @param fFile The file to be read. * * @return File contents as a string. * * @throws IOException Thrown if the input stream read failed. */ public static String readTextFileContents(File fFile) throws IOException { if (fFile == null) { throw new NullPointerException("File is null."); } Reader rReader = null; try { rReader = new FileReader(fFile); return readReaderContents(rReader); } finally { closeReader(rReader); } } /** * Reads the reader contents and returns it as a String. * * @param rReader The reader to be read. * * @return Reader contents as a string. * * @throws IOException Thrown if the read failed. */ public static String readReaderContents(Reader rReader) throws IOException { StringBuilder sbRes = new StringBuilder(2048); readReaderContents(rReader, sbRes); return sbRes.toString(); } /** * Reads the reader contents and appends them to the given StringBuilder. * * @param rReader The reader to be read. * @param sbBuffer Contents are appended to this buffer. * * @return Amount of characters read. * * @throws IOException Thrown if the read failed. */ public static long readReaderContents(Reader rReader, StringBuilder sbBuffer) throws IOException { if (rReader == null) { throw new NullPointerException("Reader is null."); } char[] caTmpBuffer = new char[1024]; long lTotal = 0; int iBytesRead; while ((iBytesRead = rReader.read(caTmpBuffer)) != -1) { sbBuffer.append(caTmpBuffer, 0, iBytesRead); lTotal += iBytesRead; } return lTotal; } /** * Closes a reader. This ignores all exceptions that might be thrown. * * @param rReader The reader to be closed. If it is null, this method does nothing. */ public static void closeReader(Reader rReader) { try { if (rReader != null) { rReader.close(); } } catch (IOException ignored) { } } }