首页 > 解决方案 > ifFile 不识别 csv、pl、sh、xml 文件

问题描述

我正在构建一个目录的递归搜索数组。这部分效果很好,但是在尝试确定文件是否为文件时,它只会将 .txt 文件添加到数组中,并跳过 csv、pl、sh、xml 等文件。我能做些什么来解决这个问题吗?这是我正在使用的代码。

            public static ArrayList<Object> listDirectory(String directory) {
                
                Object sbytes;
                File dir = new File((String) directory);
                File[] firstLevelFiles = dir.listFiles();
                ArrayList<Object> array = new ArrayList <Object>();

                 if (firstLevelFiles != null) {
                    for (File aFile : firstLevelFiles) {
            
                        //if (aFile.isFile()) {
                        if (! aFile.isDirectory()) {

                            System.out.println("[" + aFile.getAbsolutePath() + "]");
                            
                            long bytes = aFile.length();
                            if (bytes > 1000) {
                                sbytes = bytes / 1000 + " Kb";
                            } else if (bytes > 1000000){
                                sbytes = bytes / 1000000 + " Mb";
                            } else {
                                sbytes = bytes + " bytes";
                            }
                            
                            Object fileName = aFile.getName();
                            
                            Object nameAndSize = fileName + " " + sbytes;
                         
                           
                            array.add(nameAndSize);
                            
                        } else {
                       
                             ArrayList<Object> deeperList = listDirectory(aFile.getAbsolutePath());
                                array.addAll(deeperList);
                            
                        }
                    }
                }
               
                return array;

            }

标签: javatxt

解决方案


推荐阅读