public String getFileString() throws Exception {
String fileName = "C:\\Users\\fuli\\Desktop\\123.txt";
File file = new File(fileName);
StringBuilder sb = new StringBuilder();
try ( FileInputStream fis = new FileInputStream(file);InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8)){
BufferedReader bf = new BufferedReader(isr);
String content ;
while (true) {
content = bf.readLine();
if (content == null) {
break;
}
sb.append(content.trim());
}
}
return sb.toString();
}
引入org.apache.commons.io
{
String fileName = "C:\\Users\\fuli\\Desktop\\123.txt";
File file = new File(fileName);
FileUtils.readFileToString(file, "utf-8")
}