import java.io.*;
public class FileRead{
private static double totalFile = 0;
private static double totalDirectory = 0;
public String replace(String value){
StringBuffer replace = new StringBuffer(value);
int i = 0;
int last = replace.lastIndexOf("──");
i = replace.indexOf("──");
while((i != last)&&(i != -1)){
replace.replace(i,i+"──".length()," ");
i = replace.indexOf("──");
last = replace.lastIndexOf("──");
}
return replace.toString();
}
public void searchFile(File f,String value,boolean b)throws IOException{
StringBuffer string = new StringBuffer(value);
string.append("──");
boolean bool = b;
String path = f.getAbsolutePath();
File currentFile = new File(path); //取得當前路徑的文件
File[] file = currentFile.listFiles();
for(int i=0;i<file.length;i++){
StringBuffer s = null;
String lastDirectory = null;
/*
* 判斷文件夾是否為該目錄下的最后一個文件夾,如果是的話,則取消打印"│"符號
*/
for(int k=0;k<file.length;k++){
if(file[k].isDirectory())
lastDirectory = new String(file[k].getName());
}
if(file[i].getName().equals(lastDirectory)){
if(string.indexOf("│") != -1){
string.delete(string.lastIndexOf("│"),string.lastIndexOf("│")+1);
}
}
/*
* 格式化打印,將符號最后的"──"變為"├──"(當最后的符號不為"│──"時)
*/
if(!((string.lastIndexOf("──")-1) == string.lastIndexOf("│──"))){
s = new StringBuffer(string.substring(0,string.lastIndexOf("──")));
s.append("├──");
}else{
if(string.indexOf("│──")!=-1){
s = new StringBuffer(string.substring(0,string.lastIndexOf("│──")));
s.append("├──");
}
}
if(file[i].getName().equals(file[file.length-1].getName()))
if(s != null)
if(s.lastIndexOf("├") != -1)
s.replace(s.lastIndexOf("├"),s.lastIndexOf("├")+1,"└");
/*
* 如果s不為空,則將s傳入方法replace中進行格式化
*/
if(s != null)
System.out.println(replace(s.toString()) + file[i].getName());
if(file[i].isDirectory()){
totalDirectory += 1;
/*
* 如果該文件夾的子目錄下還有兩個以上的文件和文件夾,則打印一個"│"符號,并標記bool為true
*/
String pathstring = file[i].getAbsolutePath();
File current = new File(pathstring); //取得當前路徑的文件
File[] fp = current.listFiles();
if(fp.length >1){
bool = true;
}
if(bool)
string.append("│");
searchFile(file[i],string.toString(),bool);
/*
* 如果bool已經被標記過,則將上一次的"│"符號刪除
*/
if(bool)
if(string.indexOf("│") != -1)
string.delete(string.lastIndexOf("│"),string.length());
bool = false;
}
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,"│",false);
System.out.println("the file is :" + (totalFile-totalDirectory));
System.out.println("thd directory is : " + totalDirectory);
}
}
該程序存在一個問題,也就是當jdk中的File類無法判斷目錄下的一些目錄是文件夾或則是文件時?
享元模式:flyweight pattern
享元模式包括兩種狀態,內蘊狀態和外蘊狀態。他的種類有單純享元模式和復合享元模式,結構圖如下:
我們從結構種可以看出,復合享元模式是由單純享元模式和合成模式組合而成的。
享元模式有四種角色:抽象享元角色,具體享元角色,享元工廠角色,和客戶角色。享元模式中的享元工廠角色可以通過單例模式來實現。
使用條件:
裝飾模式:
他的各個角色的作用為:抽象角色:給出一個抽象的接口,以規范準備接收附加責任的對象。
具體角色:定義一個將要接收附加責任的類。
裝飾角色:持有一個構件對象的實例,并定義一個雨抽象接口一致的接口。
具體裝飾角色:負責給構件對象“貼上”附加責任。
裝飾類一般在以下情況使用:
1、 需要擴展一個類的功能,或給一個類增加附加責任。
2、 需要動態的給一個對象增加功能,這些功能可以再動態的測銷。
3、 需要增加由一些基本的排列組合產生非常大量的功能,從而使繼承關系變得不現實。
他有很多特點:
1、 裝飾模式雨繼承關系的目的都是要擴展對象的功能,但是裝飾模式可以提供比繼承更多的靈活性。裝飾模式準系統動態的決定“貼上”一個需要的“裝飾”,或者除掉一個不需要的裝飾。而繼承則不同,繼承關系是靜態的,他在系統運行前就決定了。
2、 他可以通過使用不同的具體修飾類以及這些裝飾類的排例組合,設計可以創造更多不同行為的組合。
3、 他雖然比繼承性要靈活,這意味著他比繼承更容易出錯。
缺點:由于使用裝飾模式可以比使用繼承關系需要較少數目的類,但是在另一方面,使用裝飾模式會產生比使用繼承方式更多的對象。這在使用時進行錯誤查詢變得更困難了,特別是這些對象看上去都很像。