私はここでは新人です。私は自分自身が過去の初心者主義を打ち破ったと考えており、中間プログラミングに割り込んでいます。アセットフォルダから読み込もうとしていますが、プログラムが例外をキャッチし続けるという問題があります。私が間違っていることを理解するのを手伝ってくれませんか?これが私のコードです:
package com.example.testone;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.widget.TextView;
public class AssetsTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
setContentView(textView);
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = getAssets().open("myawesometext.txt");
String text = loadTextFile(inputStream);
textView.setText(text);
} catch (IOException e) {
textView.setText("Couldn't load file");
} finally {
if (inputStream != null)
try {
inputStream.close();
} catch (IOException e) {
textView.setText("Couldn't close file");
}
}
}
public String loadTextFile(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byte[] bytes = new byte[4096];
int len = 0;
while ((len = inputStream.read(bytes)) > 0)
byteStream.write(bytes, 0, len);
return new String(byteStream.toByteArray(), "UTF8");
}
}
私はこれを含むすべてのウェブサイトを検索し、他の同様の質問をくまなく調べましたが、私に役立つ答えを見つけることができませんでした。どうぞよろしくお願いいたします。
前もって感謝します!