一.File类
1.目录列表器
示例代码1
public class DirList {
public static void main(String[] args) {
File path = new File("D:\\Work");
String[] list;
list = path.list(new DirFilter("^.*\\.rar"));
Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
for (String dirItem : list) {
System.out.println(dirItem);
}
}
}
class DirFilter implements FilenameFilter {
private String pattern;
public DirFilter(String pattern) {
this.pattern = pattern;
}
public boolean accept(File dir, String name) {
return Pattern.matches(pattern, name);
}
}
用于查询D:\work目录下所有以.rar结尾的文件
DirFilter实现FilenameFilter,以实现文件名过滤的功能,策略模式;
DirFilter实现了accept方法来过滤文件名,accept方法可以自行修改
示例代码2
public class DirList2 {
FilenameFilter filter(final String regex) {
return new FilenameFilter() {
Pattern pattern = Pattern.compile(regex);
@Override
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
};
}
public static void main(String[] args) {
File path = new File("D:\\Work");
String[] list;
FilenameFilter f = new DirList2().filter("^.*\\.rar");
list = path.list(f);
Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
for (String dirItem : list) {
System.out.println(dirItem);
}
}
}
匿名内部类实现
示例代码3
public class DirList3 {
public static void main(String[] args) {
File path = new File("D:\\Work");
String[] list;
list = path.list(new FilenameFilter() {
Pattern pattern = Pattern.compile("^.*\\.rar");
@Override
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
});
Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
for (String dirItem : list) {
System.out.println(dirItem);
}
}
}
2.目录实用工具
示例代码1
public class Directory {
public static File[] local(File dir, String regex) {
return dir.listFiles(new FilenameFilter() {
Pattern pattern = Pattern.compile("^.*\\.rar");
@Override
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
});
}
public static File[] local(String path, String regex) {
return local(new File(path), regex);
}
public static class TreeInfo implements Iterable<File> {
public List<File> files = new ArrayList<File>();
public List<File> dirs = new ArrayList<File>();
public Iterator<File> iterator() {
return files.iterator();
}
public void addAll(TreeInfo other) {
files.addAll(other.files);
dirs.addAll(other.dirs);
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("dirs:");
sb.append(dirs);
sb.append("\nfiles");
sb.append(files);
return sb.toString();
}
public static TreeInfo walk(File start, String regex) {
return recurseDirs(start, regex);
}
public static TreeInfo walk(String start, String regex) {
return walk(new File(start), regex);
}
public static TreeInfo walk(String start) {
return walk(new File(start));
}
public static TreeInfo walk(File start) {
return walk(start, ".*");
}
static TreeInfo recurseDirs(File startDir, String regex) {
TreeInfo result = new TreeInfo();
for (File item : startDir.listFiles()) {
if (item.isDirectory()) {
result.dirs.add(item);
result.addAll(recurseDirs(item, regex));
} else {
if (item.getName().matches(regex)) {
result.files.add(item);
}
}
}
return result;
}
}
public static void main(String[] args) {
System.out.println(TreeInfo.walk("D:\\学习资料"));
}
}
示例代码2
public class ProcessFile {
public interface Strategy {
void process(File file);
}
private Strategy strategy;
private String ext;
public ProcessFile(Strategy strategy, String ext) {
this.strategy = strategy;
this.ext = ext;
}
void start(String[] args) {
try {
if (args.length == 0) {
processDirectoryTree(new File("."));
} else {
for (String arg : args) {
File fileArg = new File(arg);
if (fileArg.isDirectory())
processDirectoryTree(fileArg);
else {
if (!arg.endsWith("." + ext)) {
arg += "." + ext;
}
strategy.process(new File(arg).getCanonicalFile());
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void processDirectoryTree(File root) throws IOException {
for (File file : TreeInfo.walk(root.getAbsolutePath(), ".*\\." + ext)) {
strategy.process(file.getCanonicalFile());
}
}
public static void main(String[] args) {
new ProcessFile(new Strategy() {
@Override
public void process(File file) {
System.out.println(file);
}
}, "java").start(args);
}
}
3.目录的检查及创建
public class MakeDirectories {
private static void usage() {
System.err.println("Usage:MakeDirectories path1 ...\n" + "Creates each path\n"
+ "Usage:MakeDirectories -d path1 ...\n" + "Deletes each path\n"
+ "Usage:MakeDirectories -r path1 path2\n" + "Renames from path1 to path2");
System.exit(1);
}
private static void fileData(File f) {
System.out.println("Absolute path: " + f.getAbsolutePath() + "\n Can read: " + f.canRead() + "\n Can write: "
+ f.canWrite() + "\n getName: " + f.getName() + "\n getParent: " + f.getParent() + "\n getPath: "
+ f.getPath() + "\n length: " + f.length() + "\n lastModified: " + f.lastModified());
if (f.isFile())
System.out.println("It's a file");
else if (f.isDirectory())
System.out.println("It's a directory");
}
public static void main(String[] args) {
File file = new File(".\\src\\iptables.java");
fileData(file);
}
}