FileUtil

package com.css.common.util;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 文件的读写操作
 *
 * @version 1.0
 *
 */
public final class FileUtil {

 private static Log logger = LogFactory.getLog(FileUtil.class);
 private static String CHARSET_ENCODE = "GBK";

 /**
  * 由url生成文件.
  *
  * @param urlString
  *            url地址
  * @param dir
  *            文件完全路径(包括路径,文件名,后缀)
  * @param ignoreIfExitst
  *            是否覆盖现有文件
  * @throws IOException
  */
 public static void createFileByURL(String urlString, String dir,
   boolean ignoreIfExitst) throws IOException {
  File file = new File(dir);
  if (ignoreIfExitst && file.exists()) {
   return;
  }

  StringBuffer document = new StringBuffer();
  try {
   URL url = new URL(urlString);
   URLConnection conn = url.openConnection();
   BufferedReader reader = new BufferedReader(new InputStreamReader(
     conn.getInputStream(), "utf-8"));
   String line = null;
   while ((line = reader.readLine()) != null) {
    document.append(line + "\n");
   }
   reader.close();

   FileOutputStream fos = new FileOutputStream(file);
   BufferedWriter stdout = new BufferedWriter(new OutputStreamWriter(
     fos, "utf-8"));
   stdout.write(document.toString());
   stdout.flush();
   fos.close();
   stdout.close();

  } catch (MalformedURLException e) {
   logger.error("exec(Unable to connect to URL: " + urlString + "):",
     e);
  }

  document = null;
 }

 public static String readFile(String filePath) {
  return readFile(filePath, CHARSET_ENCODE);
 }

 public static String readFile(String filePath, String charset) {
  String info = "";
  try {
   File f = new File(filePath);
   if (f.exists()) {
    FileInputStream bw = new FileInputStream(f);
    int len = bw.available();
    byte[] str = new byte[len];
    if (bw.read(str) == -1) {
     info = "";
    } else {
     info = new String(str, charset);
    }
    bw.close();
    bw = null;
   }
   f = null;
  } catch (IOException e) {
   logger.error(e);
  }
  return info;
 }

 public static void writeFile(String msg, String filePath) {
  writeFile(msg, filePath, CHARSET_ENCODE);
 }

 public static void writeFile(String msg, String filePath, String charset) {
  try {
   File file = new File(filePath);
   if (file.exists()) {
    file.delete();
   }
   FileOutputStream wf = new FileOutputStream(filePath);
   wf.write(msg.getBytes(charset));
   wf.close();
   file = null;
   wf = null;
  } catch (IOException e) {
   logger.error(e);
  }
 }

 public static void copyFile(String src, String to) {
  File srcFile = new File(src);
  if (srcFile.exists()) {
   try {
    FileInputStream bw = new FileInputStream(srcFile);
    OutputStream bos = new FileOutputStream(to);
    int bytesRead = 0;
    byte[] buffer = new byte[8192];
    while ((bytesRead = bw.read(buffer, 0, 8192)) != -1) {
     bos.write(buffer, 0, bytesRead);
    }
    bos.close();
    bw.close();
    bos = null;
    bw = null;
   } catch (FileNotFoundException ex) {
    logger.error(ex);
   } catch (IOException ex) {
    logger.error(ex);
   }
  }
 }

 public static void deleleFile(String filePath) {
  File picFile = new File(filePath);
  if (picFile.exists()) {
   picFile.delete();
  }
  picFile = null;
 }

 /**
  * @param args
  */
 //public static void main(String[] args) {
  // TODO Auto-generated method stub
//  String f = FileUtil.readFile(
//    "E:/MyProject/ec/WebRoot/html/tools/subTemplate.html", "GBK");
//  String test = "测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容"
//    + "测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容"
//    + "测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容";
//  System.out.println(f);
//  f = StringUtils.replace(f, "#{title}", "测试标题");
//  f = StringUtils.replace(f, "#{content}", test);
//  FileUtil.writeFile(f, "E:/MyProject/ec/WebRoot/html/tools/test.html",
//    "GBK");
//  System.out.println(f);
// }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值