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