2011-11-07 14 views
1

私はこのコードを試しましたが、私はいつも 'is'をNULLとして取得します。私のファイルは "res"フォルダにあります。J2MEのresフォルダからファイルを読み取る

StringBuffer str = new StringBuffer(); 
    InputStream is = getClass().getResourceAsStream(filename); 
    if(is == null) { 
     System.out.println("'is' is null"); 
    } else { 

     InputStreamReader reader = new InputStreamReader(is); 

     String line = null; 
     // Read a single line from the file. null represents the EOF. 
     while ((line = readLine(reader)) != null) { 
      // Append the read line to the main form with a linefeed ('\n') 
      str.append(line + "\n"); 
     } 
     reader.close(); 
    } 

誰かが間違っていることを知っていますか?

Thx!

+1

ファイル名の値は何ですか? –

+0

「ファイル名」を教えてもらえますか? – bharath

答えて

2

これを試してみてください:

InputStream is = getClass().getResourceAsStream("/"+filename); 
+0

私はそれを試してみます私は結果を返すよ – vicmonmena

+0

こんにちは!私はそのコードを試しました: 'StringBuffer str = new StringBuffer(); \t InputStream is = getClass()。getResourceAsStream( "/" + filename); \t if(== null){... ' 常にnullを返します。 – vicmonmena

関連する問題