-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 オブジェクトが存在しない場合、メソッド isDirectory() および isFile() は機能しません。

于 2013-07-12T07:36:46.843 に答える
0

考えられる問題は複数ありますが、考慮していません...

  • dirが存在するかどうかを確認していません
  • 読み取りエラー (またはその他の関連エラー) が発生した場合にファイルを閉じることを確認していない
  • を使用して自分の人生を困難にしていますFile#list。代わりにFile#listFiles、の配列を返す which を使用します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 に答える