/**
* 读取文本内容
* @param filePath 文件路径
* @return
*/
public static String readFileText(String filePath) {
File file = new File(filePath);
BufferedReader reader = null;
StringBuffer sb = new StringBuffer();
try {
reader = new BufferedReader(new FileReader(file));
String tempStr;
while ((tempStr = reader.readLine()) != null) {
sb.append(tempStr).append("\n");
}
reader.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return sb.toString();
}
返回List<String>
/**
* 读取文本内容
*
* @param filePath 文件路径
* @return
*/
public static List<String> readFileText(String filePath) {
File file = new File(filePath);
BufferedReader reader = null;
List<String> result = new ArrayList<>();
try {
reader = new BufferedReader(new FileReader(file));
String tempStr;
while ((tempStr = reader.readLine()) != null) {
result.add(tempStr);
}
reader.close();
return result;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return result;
}