6

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

Andrew Seymour
  • 250
  • 1
  • 4
  • 18

1 Answers1

4

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();
    }
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • Thanks for the reply. Still getting the same problem, and the Log is not present in LogCat – Andrew Seymour Jan 29 '13 at 11:05
  • 01-29 11:07:55.839: W/System.err(6791): java.io.FileNotFoundException: levelinfo.txt I have the file saved in 'assets' which is a direct subfolder of the main package – Andrew Seymour Jan 29 '13 at 11:09
  • You mean to say that you have folder inside the assets folder and in that folder your file is located? like `assets/folder/file.txt` ? Is that so ? – GrIsHu Jan 29 '13 at 11:17
  • No, it was just a very simple mistake. I read some other questions and looked for a more complicated problem rather than checking the simplest thing. Read my comment above, thanks for all the help though – Andrew Seymour Jan 29 '13 at 11:30