Here you can find the source of fileToString(String fileName)
Parameter | Description |
---|---|
fileName | a parameter |
public static String fileToString(String fileName)
//package com.java2s; //License from project: Apache License import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Main { /**/*from w w w. ja v a2 s.c o m*/ * read the file content as a String with default character encoding. * * @param fileName * @return */ public static String fileToString(String fileName) { StringBuffer stringBuffer = new StringBuffer(); try { char[] buffer = new char[1024]; FileReader reader = new FileReader(fileName); int flag = -1; while ((flag = reader.read(buffer, 0, buffer.length)) != -1) { stringBuffer.append(buffer, 0, flag); } } catch (FileNotFoundException e) { throw new IllegalArgumentException("[" + fileName + "] is not found."); } catch (IOException e) { throw new RuntimeException("Unexpected exception! caused by: " + e.getMessage(), e); } return stringBuffer.toString(); } }