gpt4 book ai didi

java - 从 Android 中的 Assets 文件夹读取文件时遇到问题

转载 作者:太空狗 更新时间:2023-10-29 15:31:05 27 4
gpt4 key购买 nike

这个问题是关于 this one 的.因为这是一个具体问题,所以我把这个问题单独移到了这里。我尝试创建一个文本文件“foo.txt”,并将其读入我的 Activity 中:

File file = new File("/assets/foo.txt");
if ( file.exists() ){
txtView.setText("Exists");
}
else{
txtView.setText("Does not exist");
}

“foo.txt”文件位于我的 Assets 文件夹中,我已验证它存在于操作系统中。我的 TextView 总是从上面的代码中获取文本“不存在”。我试过去

File file = new File("/assets/foo.txt");
Scanner in = new Scanner(file);

同样如此,但这会产生以下内联 错误:“未处理的异常类型 FileNotFoundException”。 Eclipse 然后建议涉及 try/catch,这会消除错误,但它也无法正常工作。

我也曾尝试将我的 Assets 文件夹设置为“用作源文件夹”,但这没有任何区别。我也尝试过使用原始文件夹,因为几个人建议不要使用。我别无选择,真的需要帮助。应该很容易...

另一种尝试是去

AssetManager assetManager = getResources().getAssets();
InputStream is = assetManager.open("assets/foo.txt");

但这会在第二行产生内联错误:“未处理的异常类型 IOException”。

最佳答案

在那种情况下我使用 CommonsWare(这是安全的一面 :)),但它应该是:

AssetManager assetManager = getResources().getAssets();
InputStream inputStream = null;

try {
inputStream = assetManager.open("foo.txt");
if ( inputStream != null)
Log.d(TAG, "It worked!");
} catch (IOException e) {
e.printStackTrace();
}

不要使用InputStream is = assetManager.open("assets/foo.txt");

关于java - 从 Android 中的 Assets 文件夹读取文件时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9674815/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com