Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading file from assets throwing FileNotFoundException

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

like image 208
Andrew Seymour Avatar asked Jan 29 '13 10:01

Andrew Seymour


1 Answers

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();
    }
like image 173
GrIsHu Avatar answered Oct 27 '22 06:10

GrIsHu