简体   繁体   English

java 列出子文件夹中的 txt 文件

[英]java listing the txt files in sub folders

Hi I have a jlist and currently it is viewing a folder + subfolders... Now i would like to change this to view the files in the subfolders as well.嗨,我有一个 jlist,目前它正在查看一个文件夹 + 子文件夹...现在我想更改它以查看子文件夹中的文件。 below please find the code I am currently using:请在下面找到我当前使用的代码:

 jList1.setModel(new javax.swing.AbstractListModel()
        {

            File folder = new File ("/Assignment_Datex/message_outbox/"); 
            File[] listofFiles = folder.listFiles();



       // @Override
                public int getSize() 
                { return listofFiles.length; }
       // @Override
                public Object getElementAt(int i) 
                { return listofFiles[i];}
            }

            );

在此处输入图像描述

Right now as you can see in the screenshot, the Jlist is only viewing the folders and not the files in them... Any help please?现在,正如您在屏幕截图中看到的那样,Jlist 只查看文件夹而不是其中的文件......请帮忙?

If you want to show all files and folder under some root folder then you should try someting like this...如果你想显示某个根文件夹下的所有文件和文件夹,那么你应该尝试这样的事情......

  1. Get files and folders under root folder.获取根文件夹下的文件和文件夹。
  2. Loop over them and check if it is file or folder.遍历它们并检查它是文件还是文件夹。
  3. If file then just add to list nothing more.如果是文件,则只需添加到列表中即可。
  4. If folder then add it to list and repeat this same steps for that folder until all folder and files are traveled.如果是文件夹,则将其添加到列表中并对该文件夹重复相同的步骤,直到所有文件夹和文件都被传送。

I can not produce whole code here but this is a prototype for this:我不能在这里生成完整的代码,但这是一个原型:

void addFilesToList(File folder){
   File[] listofFiles = folder.listFiles();
   for(File file:listofFile){
      if(file.isFile()) // --- file
         list.add(file.getName());
      else{             // --- folder
         addFileToList(file);
      }
   }
}

The above code is not tested so may need to modify it to fit your need.上述代码未经测试,因此可能需要对其进行修改以满足您的需要。

@Harry Joy is right. @Harry Joy 是对的。 Additionally you can also use FindFile from jakarta project.此外,您还可以使用 jakarta 项目中的 FindFile。 It can save your time.它可以节省您的时间。

I think this is good way to read all.txt files in a folder and sub folder's我认为这是读取文件夹和子文件夹中 all.txt 文件的好方法

private static void addfiles (File input,ArrayList<File> files)
    {
        if(input.isDirectory())
        {
            ArrayList <File> path = new ArrayList<File>(Arrays.asList(input.listFiles()));
            for(int i=0 ; i<path.size();++i)
            {
                if(path.get(i).isDirectory())
                {
                    addfiles(path.get(i),files);
                }
                if(path.get(i).isFile())
                {
                    String name=(path.get(i)).getName();
                    if(name.lastIndexOf('.')>0)
                    {
                        int lastIndex = name.lastIndexOf('.');
                        String str = name.substring(lastIndex);
                        if(str.equals(".txt"))
                        {
                            files.add(path.get(i));
                        }
                    }
                }
            }
        }
        if(input.isFile())
        {
            String name=(input.getName());
            if(name.lastIndexOf('.')>0)
            {
                int lastIndex = name.lastIndexOf('.');
                String str = name.substring(lastIndex);
                if(str.equals(".txt"))
                {
                    files.add(input);
                }
            }
        }

    }

Now you have a list of files that you can do some process on it!现在你有一个文件列表,你可以对它进行一些处理!

You create a constructor to initialise your class, and there you put ( tested and working )您创建一个构造函数来初始化您的 class,然后放在那里(经过测试和工作


        // initialize the class variable
        listofFiles = new ArrayList();     
        // initialize with the path
        File f = new File("/home/albertmatyi/Work/python/");
        // create a temporary list to work with
        LinkedList files = new LinkedList();
        // fill it with the contents of your path
        files.addAll(Arrays.asList(f.listFiles()));
        while (!files.isEmpty()) {
            // keep removing elements from the list
            f = files.pop();
            // if it is a directory add its contents to the files list
            if (f.isDirectory()) {
                files.addAll(Arrays.asList(f.listFiles()));
                // and skip the last if
                continue;
            }
            // check if it's a text file, and add it to listofFiles
            if (f.getName().endsWith(".txt"))
                listofFiles.add(f);
        }


EDIT: 编辑:

Note : I've changed the type of listofFiles to ArrayList<File> , which has to be initialized in the constructor using:注意:我已将 listofFiles 的类型更改为ArrayList<File> ,必须在构造函数中使用以下命令对其进行初始化:

 listofFiles = new ArrayList<File>();

This allows easier manipulation of the data - no need to manually allocate bigger space for when more text files need to be added这允许更轻松地操作数据 - 当需要添加更多文本文件时,无需手动分配更大的空间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM