读模板

//1.得取文件
String contexPath= request.getSession().getServletContext().getRealPath("/");
String filePath=contexPath+"spring"+File.separator+"xx.html";
File file = new File(filePath); //类路径
String tlp=Help.getFileString(file);

//2.读取文件

public static String getFileString(File file) {
StringBuffer sb = new StringBuffer("");
try {
InputStream fileInputStream = new FileInputStream(file);
byte[] bbuf = new byte[1024];
int hasRead = 0;
while ((hasRead = fileInputStream.read(bbuf)) != -1) {
sb.append(new String(bbuf, 0, hasRead, "utf-8"));
}
fileInputStream.close();
System.out.println(sb);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}

posted @ 2018-10-11 08:58  albert_think  阅读(129)  评论(0编辑  收藏  举报