Reading file from assets directory throws FileNotFoundException
Asked Answered
P

2

18

I'm trying to read in a text file of a bunch of words that I want to use for a word game I am writing. This list is stored in the assets directory and is a txt file. But, whenever I attempt to open it, it throws an exception.

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
}

Here's the error log, in case that's any help:

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)

Thanks everyone!

Phuongphycology answered 23/2, 2011 at 2:56 Comment(0)
N
19

Check whether the file was properly packaged in the assets folder inside the .apk file. (It can be browsed as a zip file. Rename it if necessary to look inside.)

Nonexistence answered 23/2, 2011 at 3:1 Comment(6)
Interestingly enough, the file was there when I did an export from Eclipse, but when I pulled the file off my phone (from data/app), the whole assets directory was MIA. How do I get it to create that directory?Phuongphycology
Got it! I had to right click on the assets directory in Eclipse then choose Build Path -> Use as Source Folder. Now Eclipse builds it when it packages the apk. That was a good suggestion. Thanks!Phuongphycology
Actually, I made that "great comment" mark just a little to quick... If I marked the "assets" as a Source folder, it didnt work - the file was not found. If I just let it be (standard), it worked fine =)Galatea
@YogeshMaheshwari - What didn't work for you? Perhaps if you open a question and post your code, someone can help you get it working.Nonexistence
@TedHopp - Sorry for incomplete statement.. I did tried unzipping .apk and asset folder was there but am still getting the FileNotFoundException, code was just the same, I also tried adding asset folder in source folder and even removing it cleaning project but nothing seems to workAcicula
@YogeshMaheshwari - If the asset folder (and the file(s) you want) are packaged in the .apk file, then there's something wrong with your code. Again, please post your code as a separate question and you should be able to get a solution. Adding the assets folder as a source folder is wrong -- it was just a way some people have tried so that the assets folder would be packaged in the .apk; that doesn't seem to be your problem and shouldn't be necessary in any event.Nonexistence
B
2

on Maven, the Assets folder needs to be inside projectName/ not inside projectName/src/main as suggested in other examples.

Source:

http://jayway.github.io/maven-android-plugin/generate-sources-mojo.html

Brainwork answered 13/5, 2014 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.