Here you can find the source of readFile(File file)
Read an entire file into a string.
Parameter | Description |
---|---|
file | The file |
String
.
static public final String readFile(File file) throws IOException
//package com.java2s; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.io.Reader; public class Main { /**/*w w w.j a v a 2 s. com*/ * <p>Read an entire file into a string.</p> * * @param reader The file reader * @return The contents of the file as a <code>String</code>. * @exception If an IO error occurs. */ static public final String readFile(Reader reader) throws IOException { StringBuffer strbuf = new StringBuffer(); char[] charbuf = new char[1024]; int count; while ((count = reader.read(charbuf)) > 0) { strbuf.append(charbuf, 0, count); } return strbuf.toString(); } /** * <p>Read an entire file into a string.</p> * * @param stream The input stream (e.g., from <code>class.getResourceAsStream()</code>) * @return The contents of the file as a <code>String</code>. * @exception If an IO error occurs. */ static public final String readFile(InputStream stream) throws IOException { Reader reader = new BufferedReader(new InputStreamReader(stream)); return readFile(reader); } /** * <p>Read an entire file into a string.</p> * * @param file The file * @return The contents of the file as a <code>String</code>. * @exception If an IO error occurs. */ static public final String readFile(File file) throws IOException { Reader reader = new FileReader(file); return readFile(reader); } /** * <p>Read an entire file into a string.</p> * * @param filename The file * @return The contents of the file as a <code>String</code>. * @exception If an IO error occurs. */ static public final String readFile(String filename) throws IOException { return readFile(new File(filename)); } }