随笔-19  评论-2  文章-1  trackbacks-0
  2005年8月19日
模板模式:
       
         模板方法中分两种方法:一种是模板方法,另一种是基本方法。
         模板方法:就是把基本方法组合在一起形成一个总算法或则总行为,这个模板方法一般会在抽象定义并且在子类种不加以修改的继承下来。一个抽象类可以有任意多个模板方法。
         基本方法:它又分为抽象方法,具体方法,钩子方法。
      抽象方法由抽象类申明,由子类具体实现;具体方法由抽象类申明并实现,而子类并不实现或则置换,这里面也可以有工厂方法;钩子方法,由抽象类申明并实现,但是它是一个空的实现,一般都是由子类进行扩张实现。
posted @ 2005-08-19 21:15 sky 阅读(363) | 评论 (0)编辑 收藏


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类无法判断目录下的一些目录是文件夹或则是文件时?

posted @ 2005-08-19 20:20 sky 阅读(571) | 评论 (0)编辑 收藏