文件读取



 /**
  * 读取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";
  }
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值