【问题标题】:Directory not being recognized目录未被识别
【发布时间】:2013-07-12 07:31:48
【问题描述】:

所以我有一个方法可以读取文件夹中的所有文件,并使用从文件中读取的变量在列表中创建新类。出于某种原因,它永远不会超过if(mainDir.isDirectory()){ 部分,即使路径是正确的并且我仔细检查了那里的文件夹。

public static void loadIntoClass(String dir, int temp){
    try {
        File mainDir = new File(dir);

        if(mainDir.isDirectory()){ //Checks if the dir is a folder and not a file

            String[] fileNames = mainDir.list(); //Grabs a list of all the filenames in the dir

            for(int x = 0; x > fileNames.length; x++){ //loops through all the files

                File currFile = new File(dir + fileNames[x]); //Creates the object we will be gathering information from

                if(currFile.isFile()){ //Checks to make sure the file is a file and not a folder

                    BufferedReader br = new BufferedReader(new FileReader(currFile)); 
                    String line = br.readLine();

                    int currLoop = 1;
                    boolean collides = false;

                    while(line != null){ //Will keep checking files until it reaches a blank line

                        currLoop ++; //Keeps track of how many times it loops

                        test = line.split("="); //Splits up the variable from the declaration
                        String toString = test[0].trim(); //Trims off any extra blank spaces on either side

                        System.out.println("Reading: " + toString + " on line " + currLoop); //For debugging
                        String toString2 = test[1].trim(); //Trims the second string

                        parse[currLoop] = Integer.parseInt(toString2); //Turns the string into an integer then puts it into the array

                        if(toString.equalsIgnoreCase("Collides")){
                            if(toString2.equalsIgnoreCase("true")){
                                collides = true;
                            }
                        }

                        if(toString.equalsIgnoreCase("Image Path")){
                            //path = toString2;
                        }

                        line = br.readLine();
                    }

                    if(temp == 1){
                        types.add(new Type(parse[1], parse[2], parse[3], parse[4], parse[5], parse[6], parse[7]));
                    }

                    if(temp == 2){
                        tiles.add(new Tiles(parse[1], collides, null));
                    }

                    if(temp == 3){
                        abilities.add(new Abilities(parse[1], parse[2], parse[3], parse[4]));
                    }
                    br.close();
                }
            }
        }

    } catch(Exception e) {
        System.err.println("ERROR: " + e);
    }
}

之后,如果我将其更改为“C:/test”之类的其他路径,它只会在 for 循环处冻结。这是声明:

loadIntoClass("C:/Program Files(x86)/GameNameHere/config/enemies", 1);

【问题讨论】:

  • 如果你能发布剩下的代码会很有帮助。
  • 我会担心File currFile = new File(dir + fileNames[x]),这可能会产生"C:/Program Files(x86)/GameNameHere/config/enemiessomefilename.ext",这不是您想要的。而是简单地使用mainDir.listFiles(),它将返回一个File的数组
  • 您确定您的目录名称正确吗?在 Windows 上,它通常称为 C:\Program Files (x86) 而不是 C:\Program Files(x86) - 请注意 (x86) 之前缺少的空格。
  • 或使用新文件(mainDir, fileNames[x])。
  • 1) 将catch (Exception e) { .. 形式的代码更改为catch (Exception e) { e.printStackTrace(); // very informative! .. 2) 为了尽快获得更好的帮助,请发布SSCCE

标签: java file directory


【解决方案1】:

如果底层 FS-Object 不存在,方法 isDirectory() 和 isFile() 将不起作用。

【讨论】:

    【解决方案2】:

    有多个可能的问题,您没有考虑到...

    • 您没有检查dir 是否存在
    • 您没有确保在出现读取错误(或其他相关错误)时关闭文件
    • 您使用File#list 让自己的生活变得艰难,而使用File#listFiles 将返回File 的数组
    • 更好地利用异常...

    例如...

    public static void loadIntoClass(String dir, int temp) throws IOException {
        File mainDir = new File(dir);
    
        if(mainDir.exists) { // Check to see if the abstract path actually exists
            if (mainDir.isDirectory()){ //Checks if the dir is a folder and not a file
    
                File[] fileNames = mainDir.listFiles(); //Grabs a list of all the filenames in the dir
                //String[] fileNames = mainDir.list(); //Grabs a list of all the filenames in the dir
    
                if (fileNames != null && fileNames.length > 0) {
                    //for(int x = 0; x > fileNames.length; x++){ //loops through all the files
                    for(File currFile : fileNames){ //loops through all the files
                        //File currFile = new File(dir + fileNames[x]); //Creates the object we will be gathering information from
                        if(currFile.isFile()){ //Checks to make sure the file is a file and not a folder
                            BufferedReader br = null;
                            try {
                                br = new BufferedReader(new FileReader(currFile));
                                String line = null;
    
                                int currLoop = 1;
                                boolean collides = false;
    
                                while((line = br.readLine()) != null){ //Will keep checking files until it reaches a blank line
                                    //...//
                                }
    
                                //...//
    
                            // Make sure you make all best attempts to close the reader...
                            } finally {
                                try {
                                    br.close();
                                } catch (Exception exp) {
                                }
                            }
                        }
                    }
                } else {
                    // You may not care, but...
                    throw new IOException(dir + " does not contain any files");
                }
            } else {
                throw new IOException(dir + " is not a directory");
            }
        } else {
            throw new IOException(dir + " does not exist");
        }
    }
    

    【讨论】:

    • 最小的错误最难发现;我可能可以通过在目录名称中添加一个 + "/" + 来修复它。我肯定会更改我的代码,通过添加更多如您建议的 if 和 else 以及其他内容,使其在未来不易出错。谢谢你的时间大声笑
    猜你喜欢
    • 1970-01-01
    • 2012-02-28
    • 2013-04-10
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多