gpt4 book ai didi

java - 在android源码中找不到自己创建的文件路径

转载 作者:行者123 更新时间:2023-11-29 21:55:13 30 4
gpt4 key购买 nike

我正在测试一些东西。

我在 packages/apps/Camera/中创建了 assets 文件夹,并在文件夹中添加了 test.txt 文件。

但是当我按照下面的代码 fragment 在onCreate()方法中访问文件时,发现无法获取文件。

    File file = new File("/assets/test.txt");
BufferedReader reader = null;
try {
Log.v("jerikc","read the file");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;

while ((tempString = reader.readLine()) != null) {

Log.v("jerikc","line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
Log.v("jerikc","exception");
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}

日志是:

V/jerikc (3454): 读取文件

V/jerikc (3454):异常

我想我添加了错误的路径。("/assets/test.txt") 。那么正确的道路是什么?

一些其他信息:

我的真实代码是 Util 类,没有上下文。如果我加上上下文,代码结构会有很大的变化。

谢谢。

最佳答案

你必须阅读下面的 Assets

AssetManager mAsset = context.getAssets();

InputStream is = mAsset.open("test.txt");

关于java - 在android源码中找不到自己创建的文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13377784/

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