import java.io.File;
/*
* Created on 2006-9-19
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author public2298
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public abstract class BaseFolder {
private String folderPath = null;
protected String suffix = null;
public void visit(File file)
{
if(file.isDirectory())
{
if(getCondition(file))
folderOper(file);
else
{
File[] fileArray = file.listFiles();
for(int i=0;i<fileArray.length;i++)
{
visit(fileArray[i]);
}
}
}
else
{
fileOper(file);
}
}
protected void folderOper(File file){}
protected void fileOper(File file){}
protected boolean getCondition(File file){return false;}
protected void deleteFolder(File file)
{
if(file.isDirectory())
{
File[] fileArray = file.listFiles();
for(int i=0;i<fileArray.length;i++)
{
deleteFolder(fileArray[i]);
}
file.delete();
}
else
file.delete();
}
//添加后缀名
protected void addSuffix(File file)
{
formatSuffix();
file.renameTo(new File(file.getAbsolutePath()+suffix));
}
//去掉后缀名
protected void deleteSuffix(File file)
{
formatSuffix();
String path = file.getAbsolutePath();
if(path.endsWith(suffix))
{
path = path.substring(0,path.length()-suffix.length());
file.renameTo (new File(path));
}
}
//如果后缀名不含有点,就加上点.
private void formatSuffix()
{
if((suffix!=null)&&(!suffix.startsWith(".")))
suffix = "."+suffix;
}
protected void clean()
{
}
protected void setSuffix(String suffix) {
this.suffix = suffix;
}
}
//工具类
import java.io.File;
/*
* Created on 2006-9-27
* Window - Preferences - Java - Code Style - Code Templates
*/
public class FolderUtils {
private File file;
private BaseFolder folder;
public FolderUtils(File file)
{
this.file = file;
}
public void addSuffix(String suffix)
{
folder = new AddFileSuffix();
folder.setSuffix(suffix);
folder.visit(file);
}
public void deleteSuffix(String suffix)
{
folder = new DeleteFileSuffix();
folder.setSuffix(suffix);
folder.visit(file);
}
public void deleteFileByPattern(String pattern)
{
folder = new DeleteFileByPattern(pattern);
folder.visit (file);
}
public void deleteFileBySuffix(String suffix)
{
folder = new DeleteFileBySuffix(suffix);
folder.visit(file);
}
public void deleteFodler(String pattern)
{
folder = new DeleteFolderByName(pattern);
folder.visit(file);
}
public void renameFile(long startNumber)
{
folder = new RenameFileLikeNumber(startNumber);
folder.visit(file);
}
public static void main(String[] args) {
FolderUtils util = new FolderUtils(new File("d:/src/yy"));
//util.addSuffix(".yx");
//util.deleteSuffix(".yx");
//util.deleteFileByPattern("yinxing.txt");
//util.deleteFileBySuffix(".yx");
//util.deleteFodler("yyy");
util.renameFile(999);
}
public void setFile(File file) {
this.file = file;
}
}
//如果文件是以指定名字为后缀,就删除文件的后缀
class DeleteFileSuffix extends BaseFolder {
public void fileOper(File file) {
this.deleteSuffix(file);
}
}
//如果文件增加后缀
class AddFileSuffix extends BaseFolder {
public void fileOper(File file) {
this.addSuffix(file);
}
}
//删除文件夹名和指定名字一样的文件夹,不考虑大小写.
class DeleteFolderByName extends BaseFolder {
private String pattern = null;
public DeleteFolderByName(String pattern)
{
this.pattern = pattern;
}
public void folderOper(File file)
{
deleteFolder(file);
}
public boolean getCondition(File file)
{
return file.getName().equalsIgnoreCase(pattern);
}
}
//如果文件名包含指定pattern,就将文件删除
class DeleteFileByPattern extends BaseFolder {
private String pattern = null;
public DeleteFileByPattern(String pattern)
{
this.pattern = pattern;
}
public void fileOper(File file) {
if(pattern==null)
return;
String fileName = file.getName();
if(fileName.contains(pattern))
file.delete ();
}
}
//删除具有指定后缀名的文件
class DeleteFileBySuffix extends BaseFolder {
private String suffix;
public DeleteFileBySuffix(String suffix)
{
this.suffix = suffix;
}
public void fileOper(File file) {
String path = file.getAbsolutePath();
if(path.endsWith(suffix))
file.delete();
}
}
//以依次增加的数字编号重命名文件
class RenameFileLikeNumber extends BaseFolder
{
private final String WILDCARD = "#"; //通配符
private String pattern = null;
private long startNumber = 0;
private long count = 0;
public RenameFileLikeNumber(){}
public RenameFileLikeNumber(long startNumber)
{
this.startNumber = startNumber;
count = startNumber;
}
public RenameFileLikeNumber(long startNumber,String pattern)
{
this.startNumber = startNumber;
this.pattern = pattern;
count = startNumber;
}
public void fileOper(File file) {
this.rename(file,getNameForFile());
count++;
}
private String getNameForFile()
{
String fileName = "";
if(pattern!=null)
{
int i = pattern.length();
int j = Long.toString(count).length();
for(int k=j;k<i;k++)
fileName += "0";
fileName += count+"";
return fileName;
}
else
return count+"";
}
}