package sn.demo03;
import java.io.File;
import java.io.IOException;
import net.mindview.util.Directory;
/**
* 创建一个工具,他可以在目录中穿行,并且根据Strategy对象来处理这些目录的对象(这是策略设计模式的一个实例)
*
* @author zw
*
*/
public class ProcessFiles {
public interface Strategy {
void process(File file); //处理文件的方法,用户可以根据自己的需要,自己设定处理对象集合的方法。
}
/**
* Strategy接口内嵌在ProcessFile中,使得如果你希望实现它,就必须实现ProcessFile.Strategy,他为读者提供了更多的上下文信息
* ProcessFile执行了查找具有特定扩展名的(传递给构造器的ext参数)文件所需的全部工作。并且,当它找到匹配文件时,将直接把文件传递给
* Strategy对象(也是传递给构造器的参数)。
* 如果你没有提供任何参数,那么processfile就假设你希望遍历当前目录下所有目录,你也可以指定特定的文件,带不带扩展名都可以(如果必须的话
* 他会加上扩展名),或者指定一个或者多个目录。
*/
private Strategy strategy;
private String ext;
public ProcessFiles(Strategy strategy, String ext) { //这个类的构造方法,第一个参数指的是处理某些文件的策略,第二个参数是过滤某得到指定的文件集合
this.strategy = strategy;
this.ext = ext;
}
public void start(String[] args) {
try {
if (args.length == 0) {
//new File(".") 就应该表示操作系统 shell 环境下“当前路径”映射的 File 对象~
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) {
throw new RuntimeException(e);
}
}
// 过程目录树
public void processDirectoryTree(File root) throws IOException {
for (File file : Directory.walk(root.getAbsolutePath(), ".*\\." + ext)) {
strategy.process(file.getCanonicalFile());
}
}
/**
* @param args
*/
public static void main(String[] args) {
new ProcessFiles(new ProcessFiles.Strategy() {
@Override
public void process(File file) {
System.out.println(file);
}
}, "class").start(args);
}
} /* (Excute to see output) */// :~
/**
* 我现在的水平还比较低,不能理解这段程序过深的含义,我能理解的是,使用这个工具的前提是实现它的内部接口Strategy。
* 第二个参数是通过正则表达式指定其特定的文件,其实我感觉这个工具如果在把目录这个参数释放出去,效果会更好。这样这个工具
* 就不单单应用于当前目录下了。
*/
import java.io.File;
import java.io.IOException;
import net.mindview.util.Directory;
/**
* 创建一个工具,他可以在目录中穿行,并且根据Strategy对象来处理这些目录的对象(这是策略设计模式的一个实例)
*
* @author zw
*
*/
public class ProcessFiles {
public interface Strategy {
void process(File file); //处理文件的方法,用户可以根据自己的需要,自己设定处理对象集合的方法。
}
/**
* Strategy接口内嵌在ProcessFile中,使得如果你希望实现它,就必须实现ProcessFile.Strategy,他为读者提供了更多的上下文信息
* ProcessFile执行了查找具有特定扩展名的(传递给构造器的ext参数)文件所需的全部工作。并且,当它找到匹配文件时,将直接把文件传递给
* Strategy对象(也是传递给构造器的参数)。
* 如果你没有提供任何参数,那么processfile就假设你希望遍历当前目录下所有目录,你也可以指定特定的文件,带不带扩展名都可以(如果必须的话
* 他会加上扩展名),或者指定一个或者多个目录。
*/
private Strategy strategy;
private String ext;
public ProcessFiles(Strategy strategy, String ext) { //这个类的构造方法,第一个参数指的是处理某些文件的策略,第二个参数是过滤某得到指定的文件集合
this.strategy = strategy;
this.ext = ext;
}
public void start(String[] args) {
try {
if (args.length == 0) {
//new File(".") 就应该表示操作系统 shell 环境下“当前路径”映射的 File 对象~
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) {
throw new RuntimeException(e);
}
}
// 过程目录树
public void processDirectoryTree(File root) throws IOException {
for (File file : Directory.walk(root.getAbsolutePath(), ".*\\." + ext)) {
strategy.process(file.getCanonicalFile());
}
}
/**
* @param args
*/
public static void main(String[] args) {
new ProcessFiles(new ProcessFiles.Strategy() {
@Override
public void process(File file) {
System.out.println(file);
}
}, "class").start(args);
}
} /* (Excute to see output) */// :~
/**
* 我现在的水平还比较低,不能理解这段程序过深的含义,我能理解的是,使用这个工具的前提是实现它的内部接口Strategy。
* 第二个参数是通过正则表达式指定其特定的文件,其实我感觉这个工具如果在把目录这个参数释放出去,效果会更好。这样这个工具
* 就不单单应用于当前目录下了。
*/
本文的代码来自《java编程思想-第四版》,注释是我根据书籍及网上的资料总结而写。