package com.yohn.fileupload;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
/**
* 文件操作类
*
* @version 1.0 JUN 19, 2009
* @author YOHN
* @history 1.0
*
*/
public class FileOperator
{
private List<File> lstFile = new ArrayList<File>();
private Logger log = Logger.getLogger(FileOperator.class);
/**
* 递归列出所有文件
* @param file 一个文件目录,或一个文件
* @return
* @throws FileOperatorException 文件操作异常
*/
public synchronized List<File> listFile(File file)
{
if (file.isFile())
{
lstFile.add(file);
}
else
{
for (File lsFile : file.listFiles())
{
if (lsFile.isFile())
{
lstFile.add(lsFile);
}
else
{
listFile(lsFile);
}
}
}
return lstFile;
}
/**
* 读文件内容,只对文本文件进行操作
*
* @param file 操作的文件
* @param encoding 源文件编码
* @return 返回读取的内容
* @throws FileOperatorException 文件操作异常
*/
public String fileRead(File file, String encoding)
{
long start = System.currentTimeMillis();
log.info("read 开始时间:" + start);
FileInputStream fis = null;
StringBuffer bu = new StringBuffer();
BufferedReader br = null;
InputStreamReader isr = null;
try
{
fis = new FileInputStream(file);
isr = new InputStreamReader(fis, encoding);
br = new BufferedReader(isr);
// 根据操作系统不同取换行符
String lineSeparator = System.getProperty("line.separator");
while (br.ready())
{
bu.append(br.readLine());
bu.append(lineSeparator);
}
} catch (FileNotFoundException e)
{
log.error("文件不能找到:" + e.getMessage(), e);
} catch (IOException e)
{
log.error("文件读取异常:" + e.getMessage());
} finally
{
try
{
br.close();
isr.close();
fis.close();
} catch (IOException e)
{
log.error("关闭文件流错:" + e.getMessage(), e);
}
}
long end = System.currentTimeMillis();
log.info("结束时间:" + end);
log.info("用时:" + (end - start));
return bu.toString();
}
/**
* 将文件内容转编后写入新文件,只对文本文件进行操作
*
* @param file 操作的文件
* @param writerContent 写入的内容
* @param encoding 使用什么编码
* @throws FileOperatorException 文件操作异常
*/
public void fileWrite(File file, String writerContent, String encoding)
{
long start = System.currentTimeMillis();
log.info("write 开始时间:" + start);
FileOutputStream fos = null;
Writer wr = null;
try
{
fos = new FileOutputStream(file);
wr = new OutputStreamWriter(fos, encoding);
log.debug(file.getName() + "=\n" + writerContent);
wr.write(writerContent);
} catch (IOException e)
{
log.error("写入文件时出错:" + e.getMessage(), e);
} finally
{
try
{
wr.close();
fos.close();
} catch (IOException e)
{
log.error("关闭读取流错误:" + e.getMessage(), e);
}
}
long end = System.currentTimeMillis();
log.info("结束时间:" + end);
log.info("用时:" + (end - start));
}
/**
* 二进制文件写入
* @param file 文件名
* @param content 要写入的文件
* @throws FileOperatorException
*/
public void fileWrite(File file,byte [] content){
long start = System.currentTimeMillis();
log.info("wirte 开始时间:" + start);
if(null==file || null==content || content.length==0){
log.error("缺少必要的参数[file="+file+" content="+content+"]");
}
FileOutputStream fos=null;
try
{
fos = new FileOutputStream(file);
} catch (FileNotFoundException e)
{
log.error("文件不能找到:" + e.getMessage(), e);
}
BufferedOutputStream bos = new BufferedOutputStream(fos);
try
{
bos.write(content);
fos.flush();
bos.flush();
fos.close();
bos.close();
} catch (IOException e)
{
log.error("写入文件时出错:" + e.getMessage(), e);
}
long end = System.currentTimeMillis();
log.info("结束时间:" + end);
log.info("用时:" + (end - start));
}
/**
* 二进制等文件读取类
* @param file 文件
* @return 返回读取文件的字节数
* @throws FileOperatorException
*/
public byte[] fileRead(File file) {
long start = System.currentTimeMillis();
log.info("read 开始时间:" + start);
if(null==file){
log.error("缺少必要的参数[file="+file+"]");
}
byte [] b=new byte[(int) file.length()];
try
{
FileInputStream fis=new FileInputStream(file);
BufferedInputStream bis=new BufferedInputStream(fis);
long end = System.currentTimeMillis();
bis.read(b);
log.info("结束时间:" + end);
log.info("用时:" + (end - start));
bis.close();
fis.close();
} catch (FileNotFoundException e)
{
log.error("文件不能找到:" + e.getMessage(), e);
}catch (IOException e)
{
log.error("文件读取异常:" + e.getMessage());
}
return b;
}
}