作者:
tianshi0253
链接:
http://tianshi0253.javaeye.com/blog/203419
发表时间: 2008年06月15日
声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!
包括四个文件,一个具体实现类,一个接口类,一个工厂类,一个调用类。
FileSeacher.java
package com.jqqd.ywc;
import java.io.IOException;
public interface FileSeacher {
void seacherFile(String s)throws IOException;
void seacherFolder(String s);
}
FileSeacherImpl.java
package com.jqqd.ywc;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileSeacherImpl implements FileSeacher {
static FileOutputStream outFile;
static {
try {
outFile = new FileOutputStream("D:/seach_log.dat");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void seacherFile(String s) throws IOException {
File dir = new File(s);
if (dir.exists()) {
if (dir.isFile()) {
System.out.println(s + " is an file!");
writeToFile(s);
} else {
File[] dirList = dir.listFiles();
for (int i = 0; i < dirList.length; i++) {
if (dirList[i].isFile()) {
System.out.println(dirList[i] + " is an file!");
writeToFile(dirList[i].toString() + "\n");
} else {
seacherFile(dirList[i].toString());
}
}
}
} else {
System.out.println(s + "is not existed!");
}
}
private void writeToFile(String content) {
try {
outFile.write(content.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
public void seacherFolder(String s) {
// TODO Auto-generated method stub
File dir = new File(s);
if (dir.exists()) {
if (dir.isFile()) {
System.out.println(s + " is an file!");
} else {
writeToFile(s+ " is an folder\n");
File[] dirList = dir.listFiles();
for (int i = 0; i < dirList.length; i++) {
if (dirList[i].isFile()) {
System.out.println(dirList[i].toString() + " is an file!");
} else {
writeToFile(dirList[i].toString() + " is an folder\n");
seacherFolder(dirList[i].toString());
}
}
}
}
}
}
Factory.java
package com.jqqd.ywc;
import java.io.IOException;
import com.jqqd.ywc.FileSeacherImpl;
public class Factory {
public static void seacherFile(String s)throws IOException
{
new FileSeacherImpl().seacherFile(s);
}
}
UseFactory.java
package com.jqqd.ywc;
import java.io.IOException;
import com.jqqd.ywc.Factory;
public class useFachory {
public static void main(String[] args)
{
String s = "F:/";
try {
Factory.seacherFile(s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本文的讨论也很精彩,浏览讨论>>
JavaEye推荐
文章来源:
http://tianshi0253.javaeye.com/blog/203419
posted on 2008-06-15 04:04
姚文超 阅读(218)
评论(0) 编辑 收藏