目录未被识别

时间:2013-07-12 07:31:48

标签: java file directory

所以我有一个方法可以读取文件夹中的所有文件,并在List中创建新类,并从文件中读取变量。出于某种原因,即使路径正确,我也没有超过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);

2 个答案:

答案 0 :(得分:0)

如果基础FS对象不存在,则方法isDirectory()和isFile()不起作用。

答案 1 :(得分:0)

有许多可能的问题,你没有考虑到这些问题......

  • 您没有检查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");
    }
}