import java.io.*;
public class FileRead{
private static int space=0;
private static double totalFile = 0;
private static double totalDirectory = 0;
public String printSpace(int space){
String str = "";
for(int i=0;i<space;i++)
str += " ";
return str;
}
public void searchFile(File f)throws IOException{
String path = f.getAbsolutePath();
File currentFile = new File(path); //取得當(dāng)前路徑的文件
File[] file = currentFile.listFiles();
space++;
for(int i=0;i<file.length;i++){
System.out.println(printSpace(space) + file[i].getName());
if(file[i].isDirectory()){
totalDirectory += 1;
searchFile(file[i]);
if(space>0)
space--;
}
totalFile += 1;
}
}
public static void main(String args[])throws IOException{
String path = null;
if(args.length<1)
path =".";
else
path = args[0];
FileRead read = new FileRead();
File file = new File(path);
if(!file.exists()){
System.err.print("the path is error");
System.exit(1);
}
read.searchFile(file);
System.out.println("the file is :" + (totalFile-totalDirectory));
System.out.println("thd directory is : " + totalDirectory);
}
}
這個(gè)程序在便歷多文件時(shí)會(huì)出現(xiàn)問題?不知道問題在哪里?
合成模式
合成模式他是一種樹型結(jié)構(gòu),他可以通過對(duì)合成的圖的單獨(dú)部分方法達(dá)到對(duì)整個(gè)合成圖方法問。他由三部分組成,抽象構(gòu)件—Component和樹枝構(gòu)件—Composite以及樹葉構(gòu)件Leaf。合成模式分為兩種,一種是透明式的。另一個(gè)是安全式的合成模式。
透明式的合成模式和安全式的合成模式,他們的組成成分都一樣,他們的區(qū)別是:透明式的合成模式,他在抽象構(gòu)件種增加了操作子類對(duì)象的一些接口,這樣在實(shí)現(xiàn)的過程中,無(wú)論是樹枝構(gòu)件還是樹葉構(gòu)件都必須實(shí)現(xiàn)這些接口,這對(duì)樹葉構(gòu)件來(lái)說(shuō),這種接口實(shí)現(xiàn)是多余的,所以樹葉構(gòu)件是通過平庸式的方式來(lái)實(shí)現(xiàn),這種實(shí)現(xiàn),將造成編譯時(shí)可以通過,但是當(dāng)用戶在應(yīng)用的過程過如果調(diào)用這些方法,將造成運(yùn)行時(shí)的錯(cuò)誤,這就造成了程序的不安全;而安全式的合成模式,他并沒有在抽象構(gòu)件實(shí)現(xiàn)對(duì)子結(jié)點(diǎn)接口的申明,他是通過樹枝直接對(duì)管理子類所需要的方法進(jìn)行申明,這就給程序帶來(lái)靈活性,但是也給程序帶來(lái)不便的管理性,這種實(shí)現(xiàn)使得程序不會(huì)出現(xiàn)運(yùn)行時(shí)的錯(cuò)誤。
一般我們?cè)谶@樣的情況下應(yīng)用合成模式:
1、當(dāng)我們要體現(xiàn)構(gòu)件的部分和整體的結(jié)構(gòu)時(shí)。
2、當(dāng)我們要要忽略個(gè)體構(gòu)件與整體構(gòu)件的區(qū)別時(shí),并且要平等的對(duì)待個(gè)體構(gòu)件和整體構(gòu)件時(shí)。
使用合成構(gòu)件有以下好處:
1、使用合成模式可以很容易的增加新構(gòu)件的種類。
2、使用合成模式方便了用戶對(duì)構(gòu)件的使用,因?yàn)樗麄儾挥脫?dān)心使用的是樹頁(yè)構(gòu)件還是樹枝構(gòu)件。
他的缺點(diǎn):使用合成模式之后在控制樹枝構(gòu)件的類型就不是那么容易了。
posted on 2005-08-02 01:56
sky 閱讀(176)
評(píng)論(0) 編輯 收藏