错误打开跟踪文件:目录(2)中没有这样的文件,找不到文件

时间:2012-11-11 14:47:34

标签: java android file-io inputstream trace

我是新来的。 我认为自己已经打破了新手主义,我正在进入中级编程。我试图从我的资产文件夹中读取,我遇到的问题是我的程序一直在捕获异常。你能帮我搞清楚我做错了什么吗?这是我的代码:

    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");
}

}

我搜索过包括这个在内的每个网站,梳理其他类似的问题,但找不到对我有用的答案。非常感谢您的帮助。

提前致谢!

0 个答案:

没有答案