简体   繁体   中英

Android IO reading file

I have this code in onCreate() method in starting Activity:

Parser parser = new Parser();
try {
    parser.parse();
} catch (IOException e) {
    // TODO Auto-generated catch block
}

and I have this Parser class:

public class Parser{

    public void parse(){
       BufferedReader br = new BufferedReader(new FileReader("texttoparse.txt"));
        String line;


       while ((line = br.readLine()) != null) {

            // do something on line

        }
        br.close();
    }
}

After I try run the application, I'm getting this exception: NullPointerException. Also, I've put file "texttoparse.txt" both in projects root, where AndroidManifest.xml is and in folder where both starting activity and Parser class are.

Where am I mistaking? I'm staring Android from emulator, but can I put .txt file just like that into application and use it on Android?

Raw files (like txts) should be placed in the assets folder of your Android application, and you can open streams trough getResources().getAssets().open("texttoparse.txt").

Since android does have folders for any kind of resources, you usually will not put files into the src folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM