【问题标题】:I am getting FileNotFound exception while reading the text file from Assets folder [duplicate]从 Assets 文件夹中读取文本文件时出现 FileNotFound 异常[重复]
【发布时间】:2017-04-08 18:25:01
【问题描述】:
public class intro extends AppCompatActivity {

    private StringBuilder text = new StringBuilder();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.intro);
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(
                    new InputStreamReader(getAssets().open("assets/Introduction.txt")));

            // do reading, usually loop until end of file reading
            String mLine;
            while ((mLine = reader.readLine()) != null) {
                text.append(mLine);
                text.append('\n');
            }
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(),"Error reading file!", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    //log the exception
                }
            }

            TextView output= (TextView) findViewById(R.id.intro);
            output.setText((CharSequence) text);

        }
    }
[This is my project explorer][1]}

我尝试了很多打开 assets 文件夹的路径,但它显示 filenotfound 异常。这里我在 Assets 文件夹中创建了 Introduction.txt 文件并在上面写了一些内容。但是我无法通过提供路径或文件名来打开它。

【问题讨论】:

    标签: android listview android-fragments


    【解决方案1】:

    "assets/Introduction.txt" 中删除assets/。提供给open() 的路径是您的assets/ 内的相对路径

    【讨论】:

    • 我删除了但即使它不起作用...它说 FileNotFound
    • @Chitrapandi:那么您的资产根目录中没有Introduction.txt(例如,您的模块中的src/main/assets/)。
    • 不,我在这里看到了我的项目资源管理器--->i.stack.imgur.com/OklGw.png
    • @Chitrapandi:您有一个名为 Introduction 的文件,而不是 Introduction.txt
    • 哦...我以为它会将它作为文本文件扩展名(.txt)...它的工作谢谢@CommonsWare
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多