Reading file from assets throwing FileNotFoundException
10,508
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(); }
Author by
Andrew Seymour
Full stack developer React, Angular, Redux, Node, Python Data engineer Spark, Hadoop
Updated on July 17, 2022Comments
-
Andrew Seymour almost 2 years
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 over 11 yearsThanks for the reply. Still getting the same problem, and the Log is not present in LogCat
-
Andrew Seymour over 11 years01-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
-
GrIsHu over 11 yearsYou 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 ? -
Andrew Seymour over 11 yearsNo, 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
-
GrIsHu over 11 yearsWelcome ... Glad to help you :)