2013-07-12 5 views
-1

그래서 폴더의 모든 파일을 읽고 파일에서 읽은 변수가있는 목록에 새 클래스를 만드는 메서드가 있습니다. 웬일인지 경로가 정확하고 두 번 폴더가 있는지 확인 했는데도 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 루프에서만 고정됩니다.

+0

당신이 코드의 THH 휴식을 게시 할 수있는 경우는 고원 도움이 될. – dharam

+0

나는'File currFile = new File (dir + fileNames [x])'에 대해 걱정할 것이다. 이것은 C :/Program Files (x86) /GameNameHere/config/enemiessomefilename.ext " 당신이 원하는. 대신 단순히'mainDir.listFiles()'를 사용하면'File'의 배열을 반환 할 것입니다. – MadProgrammer

+1

디렉토리의 이름을 바꿨습니까? Windows에서는 일반적으로'C : \ Program Files (x86)'이 아니라'C : \ Program Files (x86)'이라고 불립니다. – Jesper

답변

0

기본 FS 개체가없는 경우 isDirectory() 및 isFile() 메서드는 작동하지 않습니다. 당신이 고려되지 않은 다수의 가능한 문제가있다

0

...

  • dir
  • 당신이 경우 파일을 닫습니다 확인하고없는 존재하는지 확인하지 귀하 오류 (또는 기타 관련 오류를) 읽을
  • 당신 자신을 대신 EXC를보다 효율적으로 사용하는 File
  • 의 배열을 반환합니다 File#listFiles를 사용 File#list를 사용하는 생활이 어려운 만들기 eptions ... 예를 들어

...

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"); 
    } 
} 
+0

가장 작은 실수는 찾기가 가장 어렵습니다. 디렉토리 이름에 +/"+를 추가하여 문제를 해결할 수있었습니다. 나는 당신이 제안한 if와 elses를 다른 것들과 함께 추가함으로써 미래에 실수에 덜 취약하도록 코드를 확실히 바꿀 것입니다. 시간 주셔서 감사합니다 Logged – user2575449

관련 문제