/**
* 读取HTML
* @param filePath HTML地址
* @return articleContent 文章内容
*/
public static String readHTML(String filePath) {
String articleContent = "";
BufferedReader br = null;
try {
File file = new File(filePath);
if(!file.exists()){
return "";
}
// if(!file.canRead()){
// throw new Exception("权限不足");
// }
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
StringBuffer sb = new StringBuffer();
String line = "";
while((line = br.readLine())!=null){
sb.append(line);
}
articleContent = sb.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(br != null ){
br.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return articleContent;
}
/**
* 删除HTML
* @param filePath HTML地址
* @return String 处理结果(0:删除失败,1:删除成功,2:权限不足,3:系统异常)
*/
public static String removeHTML(String filePath) {
try {
File file = new File(filePath);
if(!file.exists()){
return "1";
}
// if(!file.canWrite()){
// return "2";
// }
if(file.delete()){
return "1";
}else{
return "0";
}
} catch (Exception e) {
e.printStackTrace();
return "3";
}
}
/**
* 创建HTML
* @param filePath 文件路径
* @param fileContent 文件内容
* @return String 处理结果(0:创建失败,1:创建成功,2:权限不足,3:系统异常)
*/
public static String createHTML(String filePath, String fileContent) {
BufferedWriter bw = null;
try {
File file = new File(filePath);
if(!file.exists()){
// if(!file.canWrite()){
// throw new Exception("创建文件权限不足");
// }else{
file.createNewFile();
// }
}
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
StringBuffer sb = new StringBuffer();
sb.append("<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\" pageEncoding=\"UTF-8\"%>");
sb.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
sb.append("<html>");
sb.append("<head>");
sb.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
sb.append("<meta content=\"width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\" name=\"viewport\" />");
sb.append("</head>");
sb.append("<body>");
sb.append(fileContent);
sb.append("</body>");
sb.append("</html>");
bw.write(sb.toString());
return "1";
} catch (Exception e) {
e.printStackTrace();
return "3";
}
}