有没有通过封装好的方法,直接送入文件名,就可以判断出该文件是否存在呢?
因为\upload\data\下的目录会很多,根据日期生成的!

解决方案 »

  1.   

    楼主自己修改一下吧:/*
     * 删除一个目录下的所有文件
     */   
    public static void delAllFile(String path) {
        File file = new File(path);
        if(!file.exists()) return;
        if(!file.isDirectory()) return;
        String[] tempList = file.list();
        File temp = null;
        for(int i = 0; i < tempList.length; i++) {
            if(path.endsWith(File.separator))
                temp = new File(path + tempList[i]);
            else 
                temp = new File(path + File.separator + tempList[i]);
            if(temp.isFile()) temp.delete();
            if(temp.isDirectory()) delAllFile(path + tempList[i]);  
        }
    }
      

  2.   

    下面方法返回查找文件的路径,改变一下返回值就可以得到你的效果1
    static public String Findfile(String dir, String to_find)
       {
          int i;
          String results;
          String dir_list[] = (new File(dir)).list();
          for(i = 0; i < dir_list.length; i ++ )
          {
             File to_test = new File(dir, dir_list[i]);
             if(to_test.isDirectory())
             {
                results = Findfile(to_test.getAbsolutePath(), to_find);
                if(results.length() > 0)
                   return results;
             }
             else
             {
                if((to_test.getName()).equalsIgnoreCase(to_find))
                   return to_test.getAbsolutePath();
             }
          }
          return "";
       }
      

  3.   

    只好递归搜索所有目录了。判断文件是否存在可以在创建文件对象,然后调用exsits
      

  4.   

    可以使用这样的方法class FileFinder implements FileFilter {
        String fileName;
        public FileFinder(String fileName) {
            this.fileName = fileName;
        }
        public boolean accept(File file) {
            if (file.isDirectory()) {
                URL fileURL = new URL(file.toURL(), fileName);
                File newFile = new File(fileURL.getPath());//jdk1.5例可以直接用fileURL.toURI();
                return newFile.exists();
            }
            return false;
        }
    }用的时候 File[] files = new File("c:\\upload\\data\\").listFiles(new FileFinder);
    if (files.length>0) {
       //存在
    }这样FileFinder就以Strategy模式作为可替换的组件了
      

  5.   

    如果需要查找多层
    class FileFinder implements FileFilter {
        String fileName;
        public FileFinder(String fileName) {
            this.fileName = fileName;
        }
        public boolean accept(File file) {
            if (file.isDirectory()) {
                URL fileURL = new URL(file.toURL(), fileName);
                File newFile = new File(fileURL.getPath());//jdk1.5例可以直接用fileURL.toURI();
                boolean accepted = newFile.exists();
                if (accepted) return true;
                return newFile.listFiles(new FileFinder(fileName));
            }
            return false;
        }
    }
      

  6.   

    上面错了一行
    return newFile.listFiles(new FileFinder(fileName));
    =〉
    return newFile.listFiles(new FileFinder(fileName)).length >0;