JList that contains the list of Files in a directory

前端 未结 5 1980
庸人自扰
庸人自扰 2020-12-03 16:42

I created a JList that contains a list of files that are in a directory. Here is the JList.

JList MList;
String ListData[]
// Creat         


        
5条回答
  •  执念已碎
    2020-12-03 17:18

    I don't quite understand what you're doing - it looks like you want to filter the content of the JList to be only files with the .txt extension. In that case, you're probably looking to implement a FilenameFilter that will cause list() to return a list of file names in a String[] that only matches what you specified.

    For example,

    public String[] ReadDirectory() {
        String path = "C://Documents and Settings/myfileTxt";
    
        String files = null;
        File folder = new File(path);
        FilenameFilter txtFilter = new TextFileFilter();
        String[] listOfFiles = folder.listFiles(txtFilter);
    
        return listOfFiles;
    }
    
    
    
    public class TextFileFilter extends FilenameFilter
    {
        public boolean accept(File dir, String name)
        {
            if(name != null && name.endsWith(".txt"))
            {
                return true;
            }
    
            return false;
        }
    }
    

提交回复
热议问题