私は多くのサイトの例に遭遇しましたが、ほとんどすべてが次のようなものです
:
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 に変更したところ、うまくいきました。おじゃまします。削除されなければ、誰かがこれを役に立つと思うことを願っています