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 o

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);
// }

}

目录
相关文章
zipfile.BadZipFile: File is not a zip file|4-21
zipfile.BadZipFile: File is not a zip file|4-21
zipfile.BadZipFile: File is not a zip file|4-21
|
7月前
|
Java
FileInputStream和FileOutputStream
FileInputStream和FileOutputStream
53 0
|
Java
File操作-InputStream/OutputStream及是否创建文件
File操作-InputStream/OutputStream及是否创建文件
89 0
File操作 - RandomAccessFile使用详解
File操作 - RandomAccessFile使用详解
107 0
File操作-FileReader(FileWriter)/BufferedReader(Writer)
File操作-FileReader(FileWriter)/BufferedReader(Writer)
55 0
|
存储 索引
RandomAccessFile详解
此类的实例支持对随机访问文件的读取和写入。随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组。存在指向该隐含数组的光标或索引,称为文件指针;输入操作从文件指针开始读取字节,并随着对字节的读取而前移此文件指针。
1566 0
9hutool实战:FileUtil 文件工具类(读取文件)
9hutool实战:FileUtil 文件工具类(读取文件)
2050 0
9hutool实战:FileUtil 文件工具类(读取文件)
8hutool实战:FileUtil 文件工具类(获取输入流)
8hutool实战:FileUtil 文件工具类(获取输入流)
381 0
8hutool实战:FileUtil 文件工具类(获取输入流)
10hutool实战:FileUtil 文件工具类(获取输出流)
10hutool实战:FileUtil 文件工具类(获取输出流)
695 0
10hutool实战:FileUtil 文件工具类(获取输出流)