Here you can find the source of fileToString(String filename)
Convert the content of the given file to string.
Parameter | Description |
---|---|
filename | the file to read. |
Parameter | Description |
---|---|
Exception | any exception occurs. |
public static String fileToString(String filename) throws Exception
//package com.java2s; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; public class Main { /**/*from w ww . j a v a 2 s.c o m*/ * <p> * Convert the content of the given file to string. * </p> * @param filename the file to read. * @return the content of the file. * @throws Exception any exception occurs. */ public static String fileToString(String filename) throws Exception { BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader( new FileInputStream(new File(filename)))); StringBuffer sb = new StringBuffer(); // Buffer for reading. char[] buffer = new char[1024]; int k = 0; // Read characters and append to string buffer while ((k = reader.read(buffer)) != -1) { sb.append(buffer, 0, k); } return sb.toString(); } finally { reader.close(); } } }