Java遍历目录

package test.file;

public class DirTest {
// 遍历解压
// 拷贝目录结构
// 分类
public static void main(String args[]) {
DirProcessor dirCopier = new DirProcessor();
//dirCopier.tree("a");
dirCopier.decompressDir("a_test", false);
//dirCopier.copy("a", "b", 3);
//dirCopier.classify();
}
}

package test.file;

import java.io.File;
import java.io.FileFilter;

public class DirProcessor {
private FileFilter filefilter = new CompressedFileFilter();
private DirFileFilter dirFileFilter = new DirFileFilter();

private String backupRootDir;

public void tree(String srcDir) {
File srcDirFile = new File(srcDir);
if (!srcDirFile.isDirectory()) {
System.out.println(srcDir + " is not a directory!");
return;
}

System.out.println(srcDirFile.getName());
showDirLevel(srcDirFile, 0);
}

private void showDirLevel(File srcDirFile, int level) {
File[] files = srcDirFile.listFiles();
for (File file : files) {
String fileName = file.getName();
if (file.isDirectory()) {
fileName += "/";
}
System.out.println(getLevelStr(level) + fileName);
if (file.isDirectory()) {
showDirLevel(file, level+1);
}
}
}

public void decompressDir(String rootDir, boolean display) {
File rootDirFile = new File(rootDir);
if (!rootDirFile.isDirectory()) {
System.out.println(rootDir + " is not a directory!");
return;
}

String backupRootDir = rootDir+"_backup";
File backupDirFile = new File(backupRootDir);
if (backupDirFile.exists()) {
System.out.println(backupRootDir + " dir exists!");
return;
}

//backupDirFile.mkdirs();
if (display) {
System.out.println(rootDir);
}
decompress(rootDirFile.getPath(), 0, display);
}

private void decompress(String parentDirPath, int level, boolean display) {
File parentDirFile = new File(parentDirPath);
//System.out.println("# " + parentDirPath);

// 解压目录下压缩文件
File[] files = parentDirFile.listFiles(filefilter);
for (File file : files) {
String fileName = file.getName();

if (file.isFile()) {
if (display) {
System.out.println(getLevelStr(level) + fileName); // structure
}

String srcFile = parentDirPath+"/"+fileName;
String destDir = parentDirPath+"/"+getName(fileName);
String suffix = getSuffix(fileName);
if (suffix.equalsIgnoreCase("rar")) {
File dirnameFile = new File(destDir);
if (!dirnameFile.exists()) {
if (dirnameFile.mkdirs()) {
CmdUtil.exec("unrar x " + srcFile + " -d " + destDir);
}
else {
System.out.println("Error: Failed to create dir: " + destDir);
}
}
else {
System.out.println("unrar: exists " + destDir);
}
}
else if (suffix.equalsIgnoreCase("zip")) {
File dirnameFile = new File(destDir);
if (!dirnameFile.exists()) {
CmdUtil.exec("unzip   " + srcFile + " -d " + destDir);
}
else {
System.out.println("unzip: exists " + destDir);
}
}
}
}

// 处理子目录
files = parentDirFile.listFiles(dirFileFilter);
for (File file : files) {
if (file.isDirectory()) {
if (display) {
System.out.println(getLevelStr(level) + file.getName() + "/"); // structure
}
decompress(file.getPath(), level+1, display);
}
}
}

public void copy(String srcDir, String destDir, int fileNum) {
File srcDirFile = new File(srcDir);
if (!srcDirFile.isDirectory()) {
System.out.println(srcDir + " is not a directory!");
return;
}

File destDirFile = new File(destDir);
if (destDirFile.exists()) {
System.out.println(destDir + " exists!");
return;
}

System.out.println(srcDirFile.getName());
doCopy(srcDirFile, destDirFile, fileNum, 0);
}

private void doCopy(File srcDirFile, File destDirFile, int fileNum, int level) {
//destDirFile.mkdirs();

//System.out.println("# copy " + srcDirFile.getPath() +
// " to " + destDirFile.getPath());

File[] files = srcDirFile.listFiles();
for (File file : files) {
System.out.println(getLevelStr(level) + file.getName() + " " + getSuffix(file.getName()));
if (file.isDirectory()) {
doCopy(file, file, fileNum, level+1);
}
}
}

public void classify() {

}

private String getLevelStr(int level) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < level; i++) {
sb.append("+   ");
}
sb.append("+-- ");
return sb.toString();
}

private String getSuffix(String fileName) {
        if (fileName != null) {
            int pos = fileName.lastIndexOf('.');
            if (pos > -1) {
                return fileName.substring(pos+1);
            }
        }       
        return "";
}

private String getName(String fileName) {
        if (fileName != null) {
            int pos = fileName.lastIndexOf('.');
            if (pos > -1) {
                return fileName.substring(0, pos);
            }
        }       
        return "";
}
}

package test.file;

import java.io.File;
import java.io.FileFilter;

public class DirFileFilter implements FileFilter {

@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()) {
return true;
}
return false;
}

}

package test.file;

import java.io.File;
import java.io.FileFilter;

public class CompressedFileFilter implements FileFilter {

@Override
public boolean accept(File f) {
String fileName = f.getName().toLowerCase();
if (fileName.endsWith(".rar")
|| fileName.endsWith(".zip")) {
return true;
}
return false;
}

}

package test.file;

import java.io.IOException;

public class CmdUtil {
public static void exec(String cmd) {
System.out.println("exec: " + cmd);
Runtime run = Runtime.getRuntime();
try {
Process p = run.exec(cmd);
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

你可能感兴趣的:(java)