2017-12-22 13 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; 
    } 
} 

SOLUTION:

問題は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はファイルシステムからロードするため、結果が異なります。

より具体的には、これはImageLoaderクラスのクラスパスからパスpathのファイルにcorrespoding InputStream戻り:

ImageLoader.class.getResource(path) 

を、以下がファイルシステムからファイルから読み取りReader作成:

new FileReader(path) 

同じ結果を得るには、どちらの場合も同じメカニズムを使用する必要があります。

関連する問題