/** * 读取文件内容,作为字符串返回。 * * @param fileFullName String * @param charsetName 待转向的编码,如:GBK, UTF-8, ISO-8859-1,如果为null则为默认为UTF-8 * @return String 文件内容 * @throws java.io.IOException IOException */ public static String getFileData(final String fileFullName, String charsetName) throws java.io.IOException { if(fileFullName == null || !new File(fileFullName).exists()) { log.warn(fileFullName + " 文件不存在。"); throw new IOException(fileFullName + "文件不存在。"); } BufferedInputStream input = null; StringBuilder sb = new StringBuilder(); int byteLen; charsetName = (charsetName == null) ? "UTF-8" : charsetName; try { input = new BufferedInputStream(new FileInputStream(fileFullName)); byte[] byteArr = new byte[INT_READLEN]; while((byteLen = input.read(byteArr)) != -1) { String s = new String(byteArr, 0, byteLen, charsetName); sb.append(s); } } finally { if(input != null) { input.close(); } } return sb.toString(); }