2

私は多くのサイトの例に遭遇しましたが、ほとんどすべてが次のようなものです :
http://www.java2s.com/Code/Java/J2ME/ReadDisplayFile.htm
http://www.roseindia.net/j2me/read-file.shtml
ファイル システムではなく、リソース ファイルからファイルを読み取る方法のみを示します。
これは私の現在のコードです:

InputStream is;
String path = "file:///root1/photos/a.txt"
try {
     fc = (FileConnection)Connector.open(path,Connector.READ);
     is = fc.openInputStream();
     int a = is.available();
     char buf = 0;
     String buff = new String();
     while (buf!=-1){
           buf=(char)is.read();
           buff+=buf;                      
     }          
 } catch (IOException ex) {} 

しかし、それは機能せず、無限ループが作成されます。

is.available();( int a) は 0 (なぜ?) を返し、 file:///root1/photos/a.txt存在し、以下を含みます:Hi!Hello!

どうすればこれを動作させることができますか?

編集:私はそれを理解し、(buf!=-1)-1をチェックしているunsigned charので、負になることはありません。しつこい間違い。int に変更したところ、うまくいきました。おじゃまします。削除されなければ、誰かがこれを役に立つと思うことを願っています

4

1 に答える 1

1

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

InputStream is;
String path = "file:///root1/photos/a.txt"
try {
     fc = (FileConnection)Connector.open(path,Connector.READ);
     is = fc.openInputStream();
     int a = is.available();
     char buf = 0;
     StringBuffer buff = new StringBuffer();
     int i=0;     
     String temp1=null;byte bt[]=null;   
     while ((i=is.read())!=-1){
           bt=new byte[1];
           bt[0]=(byte)i;
           temp1=new String(bt);
           buf.append(temp1);
           temp1=null;
           bt=null;

     }          
 } catch (IOException ex) {} 

bufは、文字列を持つ文字列バッファです。

于 2012-05-01T08:20:53.870 に答える