gpt4 book ai didi

android - 从 Assets 目录读取文件抛出 FileNotFoundException

转载 作者:IT老高 更新时间:2023-10-28 23:11:00 31 4
gpt4 key购买 nike

我正在尝试读取一个包含一堆单词的文本文件,这些单词要用于我正在编写的文字游戏。此列表存储在 assets 目录中,是一个 txt 文件。但是,每当我尝试打开它时,它都会引发异常。

List<String>wordList = new ArrayList<String>();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open("wordlist.txt"))); //throwing a FileNotFoundException?
String word;
while((word=br.readLine()) != null)
wordList.add(word); //break txt file into different words, add to wordList
}
catch(IOException e) {
e.printStackTrace();
}
finally {
try {
br.close(); //stop reading
}
catch(IOException ex) {
ex.printStackTrace();
}
}
String[]words = new String[wordList.size()];
wordList.toArray(words); //make array of wordList

for(int i=0;i<words.length; i++)
Log.i("Brian", words[i]); //print out words in array
}

这是错误日志,以防万一:

02-22 20:49:47.646: WARN/System.err(2351): java.io.FileNotFoundException: wordlist.txt
02-22 20:49:47.646: WARN/System.err(2351): at android.content.res.AssetManager.openAsset(Native Method)
02-22 20:49:47.746: WARN/System.err(2351): at android.content.res.AssetManager.open(AssetManager.java:299)
02-22 20:49:47.746: WARN/System.err(2351): at android.content.res.AssetManager.open(AssetManager.java:273)
02-22 20:49:47.756: WARN/System.err(2351): at com.bic.anagram.GameActivity.onCreate(GameActivity.java:40)
02-22 20:49:47.756: WARN/System.err(2351): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-22 20:49:47.756: WARN/System.err(2351): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
02-22 20:49:47.756: WARN/System.err(2351): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
02-22 20:49:47.766: WARN/System.err(2351): at android.app.ActivityThread.access$2400(ActivityThread.java:121)
02-22 20:49:47.766: WARN/System.err(2351): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1925)
02-22 20:49:47.766: WARN/System.err(2351): at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 20:49:47.776: WARN/System.err(2351): at android.os.Looper.loop(Looper.java:136)
02-22 20:49:47.776: WARN/System.err(2351): at android.app.ActivityThread.main(ActivityThread.java:4425)
02-22 20:49:47.776: WARN/System.err(2351): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 20:49:47.776: WARN/System.err(2351): at java.lang.reflect.Method.invoke(Method.java:521)
02-22 20:49:47.776: WARN/System.err(2351): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-22 20:49:47.776: WARN/System.err(2351): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-22 20:49:47.776: WARN/System.err(2351): at dalvik.system.NativeStart.main(Native Method)
02-22 20:49:47.776: WARN/dalvikvm(2351): threadid=3: thread exiting with uncaught exception (group=0x4001e280)

谢谢大家!

最佳答案

检查文件是否正确打包在 .apk 文件内的 assets 文件夹中。 (它可以以 zip 文件的形式浏览。如有必要,请重命名。)

关于android - 从 Assets 目录读取文件抛出 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5086539/

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