Here you can find the source of readStringFromFile(String fileName)
Parameter | Description |
---|---|
fileName | the file |
Parameter | Description |
---|---|
IOException | an exception |
public static String readStringFromFile(String fileName) throws IOException
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.net.URL; import java.util.UUID; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import net.sf.json.JSON; import net.sf.json.JSONObject; import org.apache.log4j.Logger; public class Main{ /**//from w ww. j a va2 s .com * Reads the entire file and returns its content as a single {@link String} * * @param fileName the file * @return the file content as String * @throws IOException */ public static String readStringFromFile(String fileName) throws IOException { return readStringFromBufferedReader(createBufferedUtf8Reader(fileName)); } /** * Reads all content from the given {@link Reader} and returns it as a single {@link String} * * @param br the file * @return the file content as String * @throws IOException */ private static String readStringFromBufferedReader(BufferedReader br) throws IOException { StringBuffer sbr = new StringBuffer(); for (String ln = br.readLine(); ln != null; ln = br.readLine()) sbr.append(ln + "\n"); br.close(); return sbr.toString(); } /** * Opens a file given by a path and returns its {@link BufferedReader} using the * UTF-8 encoding * * @param path path to a file to be read * @return UTF8 BufferedReader of the file <tt>path</tt> * @throws IOException */ public static BufferedReader createBufferedUtf8Reader(String path) throws IOException { return createBufferedUtf8Reader(new File(path)); } /** * Opens a file given by a path and returns its {@link BufferedReader} using the * UTF-8 encoding * * @param file file to be read * @return UTF8 BufferedReader of the <tt>file</tt> * @throws IOException */ public static BufferedReader createBufferedUtf8Reader(File file) throws IOException { return createBufferedUtf8Reader(new FileInputStream(file)); } /** * Opens a URL and returns its {@link BufferedReader} using the UTF-8 encoding * * @param url to be read * @return UTF8 BufferedReader of the <tt>url</tt> * @throws IOException */ public static BufferedReader createBufferedUtf8Reader(URL url) throws IOException { return createBufferedUtf8Reader(url.openStream()); } /** * Creates a {@link BufferedReader} on the top of the given {@link InputStream} using the * UTF-8 encoding * * @param is file to be read * @return UTF8 BufferedReader of the <tt>file</tt> * @throws IOException */ public static BufferedReader createBufferedUtf8Reader(InputStream is) throws IOException { return new BufferedReader(new InputStreamReader(is, "utf8")); } }