【发布时间】: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。