2017-12-22 5 views
0

png 및 txt 파일을 열고 읽는 프로그램을 만들고 있습니다. 이건 내 코드입니다 :시스템이 java에서 지정된 txt 파일을 찾을 수 없습니다.

\textures\lvl1.txt (The system cannot find the file specified) 
\textures\lvl2.txt (The system cannot find the file specified) 
\textures\lvl3.txt (The system cannot find the file specified) 
\textures\lvl4.txt (The system cannot find the file specified) 
\textures\lvl5.txt (The system cannot find the file specified) 

내 파일이 lvl1 ... 5.txt 및 메뉴 ... levelOptions.png가 있었다 :

public static void init() { 
     //... 
     //compiler are finding a path for png files... 
     menu = ImageLoader.loadImage("/textures/menu.png"); 
     options = ImageLoader.loadImage("/textures/options.png"); 
     level = ImageLoader.loadImage("/textures/levelmenu.png"); 
     levelOptions = ImageLoader.loadImage("/textures/leveloptions.png"); 

     //..., but no for txt 
     map[0] = new LoadMap("/textures/lvl1.txt"); 
     map[1] = new LoadMap("/textures/lvl2.txt"); 
     map[2] = new LoadMap("/textures/lvl3.txt"); 
     map[3] = new LoadMap("/textures/lvl4.txt"); 
     map[4] = new LoadMap("/textures/lvl5.txt"); 
     //... 
} 

하지만 난 그것을 실행할 때, 나는이 오류 같은 디렉토리

LoadMap 생성자 :

public LoadMap(String path) { 
    try { 
    BufferedReader reader = new BufferedReader(new FileReader(path)); 

    String s = " "; 
    s = reader.readLine(); 
    String[] wordsXY = s.split(" "); 
    x = wordsXY[0]; 
    iX = Integer.parseInt(x); 
    y = wordsXY[1]; 
    iY = Integer.parseInt(y); 

    while ((s = reader.readLine()) != null) { 
     String[] words = s.split(" "); 
     for (int i = 0; i < iY; i++) { 
     arrayList.add(Integer.parseInt(words[i])); 
     } 
    } 
    reader.close(); 
    } catch (IOException e) { 
    System.out.println(e.getMessage()); 
    } 
} 

하여 ImageLoader 클래스 :

public class ImageLoader { 

    public static BufferedImage loadImage(String path) { 
     try { 
      return ImageIO.read(ImageLoader.class.getResource(path)); 
     } catch (IOException e) { 
      System.out.println(e.getMessage()); 
      System.exit(1); 
     } 
     return null; 
    } 
} 

해결책 :

문제는 loadMap 클래스에 있었다. 대신

:

BufferedReader reader = new BufferedReader(new FileReader(path)); 

가 있어야한다 : 도움을

InputStream is = getClass().getResourceAsStream(path); 
InputStreamReader isr = new InputStreamReader(is); 
BufferedReader reader = new BufferedReader(isr); 

감사합니다.

+3

'LoadMap'코드를 공유 할 수 있습니까? – Mureinik

+1

글쎄, 그 파일들을 찾을 수없는 것처럼 들리 네. 실제로 * 루트 디렉토리에'textures' 디렉토리가 있습니까? 그럴 가능성은 희박합니다. 'loadImage'가'Class.getResource'를 어떻게 사용하는지 주목하십시오. 이것은'FileReader'를 생성하는 것과 매우 다릅니다. –

+0

당신 말이 맞아요. 내 텍스처 폴더 디렉토리는 myproject/res/textures/png & txt 파일입니다. 메서드 loadImage res 폴더를 건너 뜁니다. 이제, 언제 : map [0..5] = 새로운 LoadMap ("res/textures/lvl1.txt") 괜찮습니까? – Descartes

답변

1

ImageLoader은 클래스 패스에서 리소스를로드하지만, LoadMap은 파일 시스템에서로드하므로 차이가 발생합니다.

이보다 구체적으로, 이것이 InputStreamImageLoader 클래스의 클래스 경로에서 경로 path와 파일에 correspoding 반환

ImageLoader.class.getResource(path) 

그리고 다음은 파일 시스템에서 파일에서 읽는 Reader 작성

new FileReader(path) 

동일한 결과를 얻으려면 두 경우 모두 동일한 메커니즘을 사용해야합니다.

관련 문제