import java.util.Vector;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-8-2 16:15:03
* 文件夹角色
*/
public class Folder implements IFile {
private String name; //文件名字
private int deep; //层级深度,根深度为0
private Vector componentVector = new Vector();
public Folder(String name) {
this.name = name;
}
//返回自己的实例
public IFile getComposite() {
return this;
}
//某个商业方法
public void sampleOperation() {
System.out.println("执行了某个商业方法!");
}
//增加一个文件或文件夹
public void add(IFile IFile) {
componentVector.addElement(IFile);
IFile.setDeep(this.deep + 1);
}
//删除一个文件或文件夹
public void remove(IFile IFile) {
componentVector.removeElement(IFile);
}
//返回直接子文件(夹)集合
public Vector getAllComponent() {
return componentVector;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDeep() {
return deep;
}
public void setDeep(int deep) {
this.deep = deep;
}
}
--------------------next---------------------
import java.util.Iterator;
import java.util.Vector;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-8-2 16:35:25
* 遍历树的一个测试
*/
public class Client {
public static String indentChar = "\t"; //文件层次缩进字符
public static void main(String args[]) {
new Client().test();
}
/**
* 客户端测试方法
*/
public void test() {
//根下文件及文件夹
Folder root = new Folder("树根");
Folder b1_1 = new Folder("1_枝1");
Folder b1_2 = new Folder("1_枝2");
Folder b1_3 = new Folder("1_枝3");
File l1_1 = new File("1_叶1");
File l1_2 = new File("1_叶2");
File l1_3 = new File("1_叶3");
//b1_2下的文件及文件夹
Folder b2_1 = new Folder("2_枝1");
Folder b2_2 = new Folder("2_枝2");
File l2_1 = new File("2_叶1");
//缔造树的层次关系(简单测试,没有重复添加的控制)
root.add(b1_1);
root.add(b1_2);
root.add(l1_1);
root.add(l1_2);
b1_2.add(b2_1);
b1_2.add(b2_2);
b1_2.add(l2_1);
root.add(l1_3);
root.add(b1_3);
//控制台打印树的层次
outTree(root);
}
public void outTree(Folder folder) {
System.out.println(folder.getName());
iterateTree(folder);
}
/**
* 遍历文件夹,输入文件树
*
* @param folder
*/
public void iterateTree(Folder folder) {
Vector clist = folder.getAllComponent();
//todo:遍历之前可以对clist进行排序,这些都不是重点
for (Iterator it = clist.iterator(); it.hasNext();) {
IFile em = it.next();
if (em instanceof Folder) {
Folder cm = (Folder) em;
System.out.println(getIndents(em.getDeep()) + cm.getName());
iterateTree(cm);
} else {
System.out.println(getIndents(em.getDeep()) + ((File) em).getName());
}
}
}
/**
* 文件层次缩进字符串
*
* @param x 缩进字符个数
* @return 缩进字符串
*/
public static String getIndents(int x) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < x; i++) {
sb.append(indentChar);
}
return sb.toString();
}
}
--------------------next---------------------
阅读(104) | 评论(0) | 转发(0) |