写了个Java操作文件的简单例子,希望给需要帮助的兄弟提供点儿帮助,同时也希望大家给予批评指正。
文件操作类,代码如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
public class FileTools {
private static final String SPECIAL_FILE_STYLE_PICTURECACHE = "Thumbs.db";
public static final Logger logger = LogManager.getLogger(FileTools.class);
public FileTools()
{
}
/*
* @discription:Delete a folder
* @param:dirPath String
* @return boolean
*/
public static boolean deleteDirectory(String dirPath,boolean deteDirRoot)
{
File parentFile = new File(dirPath);
File[] chirenFile = null;
if(parentFile.exists() && parentFile.isDirectory())
{
chirenFile = parentFile.listFiles();
for (int i = 0; i < chirenFile.length; i++) {
if(!chirenFile[i].isDirectory())
{
chirenFile[i].delete();
}
else
{
deleteDirectory(chirenFile[i].getAbsolutePath(),true);
chirenFile[i].delete();
}
}
if(deteDirRoot)
{
parentFile.delete();
}
return true;
}
else
{
FileTools.logger.error("The Floder Is Not Exist!");
return false;
}
}
/*
* @discription:Copy Folder
* @param:sourceFilePath String,targetFilePath String
* @return boolean
*/
public static boolean copyDirectory(String sourceFilePath,String targetFilePath) throws IOException
{
File sourceFile = new File(sourceFilePath);
File targetFile = new File(targetFilePath);
if(!targetFile.exists())
{
targetFile.mkdir();
}
File[] sourceChirenFile = null;
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
FileTools.logger.debug("Copy "+sourceFile.getName()+" Floder Start......");
if(sourceFile.exists() && sourceFile.isDirectory())
{
sourceChirenFile = sourceFile.listFiles();
for (int i = 0; i < sourceChirenFile.length; i++) {
File tempFile = null;
if(!sourceFilePath.endsWith(File.separator))
{
tempFile = new File(targetFilePath+File.separator+sourceChirenFile[i].getName());
}
else
{
tempFile = new File(targetFilePath+sourceChirenFile[i].getName());
}
if(!FileTools.SPECIAL_FILE_STYLE_PICTURECACHE.equalsIgnoreCase(sourceChirenFile[i].getName()))
{
if(!sourceChirenFile[i].isDirectory())
{
fileInputStream = new FileInputStream(sourceChirenFile[i]);
fileOutputStream = new FileOutputStream(tempFile);
byte[] b = new byte[1024];
FileTools.logger.debug("Copy "+sourceChirenFile[i].getName()+" File Start......");
while(fileInputStream.read(b)!=-1)
{
fileOutputStream.write(b);
}
FileTools.logger.debug("Copy "+sourceChirenFile[i].getName()+" File End!");
fileInputStream.close();
fileOutputStream.close();
}
else
{
copyDirectory(sourceFilePath+File.separator+sourceChirenFile[i].getName(),
targetFilePath+File.separator+sourceChirenFile[i].getName());
}
}
}
FileTools.logger.debug("Copy "+sourceFile.getName()+" Floder End!");
return true;
}
else
{
FileTools.logger.error("The Floder Is Not Exist!");
return false;
}
}
}
Junit测试类
import java.io.IOException;
import org.coolyongzi.FileTools;
import junit.framework.TestCase;
public class FileToolsTest extends TestCase {
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testDeleteDirectory()
{
FileTools.deleteDirectory("F:/复件 图片",false);
}
public void testCopyDirectory()
{
try {
FileTools.copyDirectory("F:/Game","F:/Game1");
} catch (IOException e) {
e.printStackTrace();
}
}
}
posted on 2010-08-24 14:52
fly 阅读(158)
评论(0) 编辑 收藏 所属分类:
java学习