我怎么能读取java中的多个文件夹,并应该相应地返回每个文件夹?

时间:2015-02-25 05:34:00

标签: java

File file = new File(java.getProperty("db.filenew"));
File[] files = file.listFiles();

for (int i = 0; i < files.length; i++) {
    // for (File file1 : files) {

    if (files[i].isDirectory()) 
       files[i].getCanonicalPath();

    if (files[i].isFile()) 
       files[i].getPath();
    files[i].getName().endsWith(".xml");

这里我正在阅读来自.properites.i的文件已经使用了以下片段来读取该特定文件夹中的每个文件,但它只返回文件夹而不是文件。所以请帮助我。

    public void readfile(File file) {
        file.getPath();
        File fileattribute = null;
        if (fileattribute.isFile()) {
            this.readfile(fileattribute);
        }

    }

    public File readdirectory(File fi) throws InterruptedException,
    ClassNotFoundException, SQLException {

        File sub[] = fi.listFiles();

        for (File f_ar: sub) {

            if (f_ar.isDirectory()) {

                this.readdirectory(f_ar);

            }


            return fi;
        }

2 个答案:

答案 0 :(得分:-1)

在read目录方法中,在for循环中为if语句添加另一个子句,以检查File是否为文件,然后调用read file方法。

public File readdirectory(File fi) throws InterruptedException, ClassNotFoundException, SQLException     
{     
    File sub[] = fi.listFiles(); 
    for (File f_ar : sub) 
    { 
        if (f_ar.isDirectory())    
        { 
              this.readdirectory(f_ar); 
        }
        else
        {
              this.readfile(f_ar);
        }
    }
}

答案 1 :(得分:-1)

使用类来存储文件夹和文件列表

例如:

  1. 为文件夹
  2. 创建一个类
    
        class Folder
    {
        private List folders = new ArrayList();
        private List files = new ArrayList();
        private String name;
        private String path;
    
    
        public List getFolders() {
            return folders;
        }
    
        public void setFolders(List folders) {
            this.folders = folders;
        }
    
        public List getFiles() {
            return files;
        }
    
        public void setFiles(List files) {
            this.files = files;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPath() {
            return path;
        }
    
        public void setPath(String path) {
            this.path = path;
        }
    
        public Folder(String name, String path) {
            this.name = name;
            this.path = path;
        }
    
        public void addFolder(Folder subfolder) {
            folders.add(subfolder);
    
        }
    
        public void addFolderFile(String name, String canonicalPath) {
            files.add(new FolderFile(name, canonicalPath));
    
        }
    
        @Override
        public String toString() {
            return "Folder [name=" + name + ", path=" + path + "]";
        }
    
    
    }
    
    1. 为文件

      创建一个类
      
      class FolderFile
      {
      private String name;
      private String path;
      
      
      public String getName() {
          return name;
      }
      
      public void setName(String name) {
          this.name = name;
      }
      
      public String getPath() {
          return path;
      }
      
      public void setPath(String path) {
          this.path = path;
      }
      
      public FolderFile(String name, String path) {
          this.name = name;
          this.path = path;
      }
      
      @Override
      public String toString() {
          return "FolderFile [name=" + name + ", path=" + path + "]";
      }
      
      
      }
      
    2. 为根文件夹

    3. 创建文件夹类的实例
      
       File root = new File(java.getProperty("db.filenew"));
              Folder folder = null;
              if(root.exists())
              {
                  File[] allFiles = root.listFiles();
                  folder = new Folder(args[0], root.getCanonicalPath());
                  readDirectory(folder, allFiles);
      
              }
      
      1. 阅读目录
      2. 
         private  void readDirectory(Folder folder, File[] allFiles) throws IOException {
        
                for (File file : allFiles) {
                    if(file.isDirectory())
                    {
                        Folder subfolder = new Folder(file.getName(), file.getCanonicalPath());
                        folder.addFolder(subfolder);
                        readDirectory(subfolder, file.listFiles());
        
                    }
                    else
                    {
                        folder.addFolderFile(file.getName(),file.getCanonicalPath());
                    }
                }
            }
        
        1. 从文件夹对象
        2. 中获取信息
          readFolder(folder);
          1. 读取文件夹对象
          2. private void readFolder(Folder folder) { System.out.println(folder.toString()); //List of Folder List folders = folder.getFolders(); System.out.println("Sub-Folder under Folder :" + folder.getPath()); System.out.println("*******************************************");

                for (Folder subfolder : folders) {
                    System.out.println(subfolder.toString());
                    readFolder(subfolder);
                }
                //List of Files
                List<FolderFile> files = folder.getFiles();
                System.out.println("File under Folder :" + folder.getPath());
                System.out.println("*******************************************");
                for (FolderFile folderFile : files) {
            
            
                    System.out.println(folderFile);
                }
            
            
            }
            

相关问题