求thing in java第4版第18章练习6的答案 java 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 主方法类Test.java:import java.io.*;public class Test { public interface Strategy { void process(File file); } private Strategy strategy; private String ext; public Test(Strategy strategy, String ext) { this.strategy = strategy; this.ext = ext; } public 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 { // Allow user to leave off extension: 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()); } } // Demonstration of how to use it: public static void main(String[] args) { new Test(new Test.Strategy() { public void process(File file) { System.out.println(file); } }, "java").start(args); }} /* (Execute to see output) *///:~Directory.javaimport java.util.regex.*;import java.io.*;import java.util.*;public final class Directory { public static File[] local(File dir, final String regex) { return dir.listFiles(new FilenameFilter() { private Pattern pattern = Pattern.compile(regex); public boolean accept(File dir, String name) { return pattern.matcher( new File(name).getName()).matches(); } }); } public static File[] local(String path, final String regex) { // Overloaded return local(new File(path), regex); } // A two-tuple for returning a pair of objects: public static class TreeInfo implements Iterable<File> { public List<File> files = new ArrayList<File>(); public List<File> dirs = new ArrayList<File>(); // The default iterable element is the file list: public Iterator<File> iterator() { return files.iterator(); } void addAll(TreeInfo other) { files.addAll(other.files); dirs.addAll(other.dirs); } public String toString() { return "dirs: " + PPrint.pformat(dirs) + "\n\nfiles: " + PPrint.pformat(files); } } public static TreeInfo walk(String start, String regex) { // Begin recursion return recurseDirs(new File(start), regex); } public static TreeInfo walk(File start, String regex) { // Overloaded return recurseDirs(start, regex); } public static TreeInfo walk(File start) { // Everything return recurseDirs(start, ".*"); } public static TreeInfo walk(String start) { return recurseDirs(new File(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 // Regular file if (item.getName().matches(regex)) { result.files.add(item); } } return result; } // Simple validation test: public static void main(String[] args) { if (args.length == 0) { System.out.println(walk(".")); } else { for (String arg : args) { System.out.println(walk(arg)); } } }} ///:~PPrint.javaimport java.util.*;public class PPrint { public static String pformat(Collection<?> c) { if (c.size() == 0) { return "[]"; } StringBuilder result = new StringBuilder("["); for (Object elem : c) { if (c.size() != 1) { result.append("\n "); } result.append(elem); } if (c.size() != 1) { result.append("\n"); } result.append("]"); return result.toString(); } public static void pprint(Collection<?> c) { System.out.println(pformat(c)); } public static void pprint(Object[] c) { System.out.println(pformat(Arrays.asList(c))); }} ///:~ 关于String用new初始化和不用new初始化区别? JFrame调用removeAll()后再重加组件显示不出,怎么办啊 关于动态生产的文本框,如果获取用户在里面输入的值? Java初级问题..关于switch case 的.请进... 用JAVA语言提取Sun LDAP中的密码, 怎么提取不出来啊? 有谁能帮帮我? 问一个关于java客户端软件的问题 有个弱问题 JTable 中的事件操作 请问哪里有ibm的java虚拟机下载? file not found的问题 关于邮件附件的RFC协议是那个 org.w3c.dom.Node的getChildNodes()方法的迷惑
}
private Strategy strategy;
private String ext; public Test(Strategy strategy, String ext) {
this.strategy = strategy;
this.ext = ext;
} public 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 {
// Allow user to leave off extension:
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());
}
} // Demonstration of how to use it:
public static void main(String[] args) {
new Test(new Test.Strategy() {
public void process(File file) {
System.out.println(file);
}
}, "java").start(args);
}
} /* (Execute to see output) *///:~Directory.javaimport java.util.regex.*;
import java.io.*;
import java.util.*;public final class Directory { public static File[]
local(File dir, final String regex) {
return dir.listFiles(new FilenameFilter() {
private Pattern pattern = Pattern.compile(regex); public boolean accept(File dir, String name) {
return pattern.matcher(
new File(name).getName()).matches();
}
});
} public static File[]
local(String path, final String regex) { // Overloaded
return local(new File(path), regex);
} // A two-tuple for returning a pair of objects:
public static class TreeInfo implements Iterable<File> { public List<File> files = new ArrayList<File>();
public List<File> dirs = new ArrayList<File>(); // The default iterable element is the file list:
public Iterator<File> iterator() {
return files.iterator();
} void addAll(TreeInfo other) {
files.addAll(other.files);
dirs.addAll(other.dirs);
} public String toString() {
return "dirs: " + PPrint.pformat(dirs)
+ "\n\nfiles: " + PPrint.pformat(files);
}
} public static TreeInfo
walk(String start, String regex) { // Begin recursion
return recurseDirs(new File(start), regex);
} public static TreeInfo
walk(File start, String regex) { // Overloaded
return recurseDirs(start, regex);
} public static TreeInfo walk(File start) { // Everything
return recurseDirs(start, ".*");
} public static TreeInfo walk(String start) {
return recurseDirs(new File(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 // Regular file
if (item.getName().matches(regex)) {
result.files.add(item);
}
}
return result;
} // Simple validation test:
public static void main(String[] args) {
if (args.length == 0) {
System.out.println(walk("."));
} else {
for (String arg : args) {
System.out.println(walk(arg));
}
}
}
} ///:~PPrint.javaimport java.util.*;public class PPrint { public static String pformat(Collection<?> c) {
if (c.size() == 0) {
return "[]";
}
StringBuilder result = new StringBuilder("[");
for (Object elem : c) {
if (c.size() != 1) {
result.append("\n ");
}
result.append(elem);
}
if (c.size() != 1) {
result.append("\n");
}
result.append("]");
return result.toString();
} public static void pprint(Collection<?> c) {
System.out.println(pformat(c));
} public static void pprint(Object[] c) {
System.out.println(pformat(Arrays.asList(c)));
}
} ///:~