-1

所以我有一个方法可以读取文件夹中的所有文件,并使用从文件中读取的变量在 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);
4

2 回答 2

0

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

于 2013-07-12T07:36:46.843 回答
0

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

  • 您没有检查是否dir存在
  • 如果出现读取错误(或其他相关错误),您不能确保关闭文件
  • 你让自己的生活变得艰难File#list,而是使用File#listFileswhich 将返回一个数组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");
    }
}
于 2013-07-12T07:45:54.680 回答