Reading file from assets throwing FileNotFoundException

Andrew Seymour picture Andrew Seymour · Jan 29, 2013 · Viewed 9.9k times · Source

I'm using the following code:

    public void readLevel(int line){
    AssetManager am = this.getAssets();
    InputStream is = null;
    try {
        is = am.open("levelinfo.txt");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Scanner scanner = new Scanner(is);
    scanner.useDelimiter(",");
    String skip;
    for(int i = 1; i < line; i++){
        skip = scanner.nextLine();
    }
    levelData = new ArrayList<Integer>();
    while(scanner.hasNextInt()){
        levelData.add(scanner.nextInt());
    }
}

The code is giving a FileNotFoundException. I've seen some similar problems, but I'm not quite sure how to solve it. The file is a text file inside the assets folder. Any help is appreciated

Andy

Answer

GrIsHu picture GrIsHu · Jan 29, 2013

Try out this way:

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