public String readTxtFile(String filePath){
StringBuffer buffer=new StringBuffer();
try {
String encoding="utf-8";
File file=new File(filePath);
//判断文件是否存在
if(file.isFile() && file.exists()){
InputStreamReader read = new InputStreamReader(
//考虑到编码格式
new FileInputStream(file),encoding);
String lineTxt = null;
BufferedReader bufferedReader = new BufferedReader(read);
while((lineTxt = bufferedReader.readLine()) != null){
buffer.append(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return buffer.toString();
}
读取文件
最新推荐文章于 2023-08-08 08:20:52 发布