3
public void loadFile(int level){ try { //Create new file levelFile = new File("assets/levels.txt"); fis = new FileInputStream(levelFile); isr = new InputStreamReader(fis); reader = new BufferedReader(isr); //Code to read the file goes here 

Using this code, however, I keep getting the above error (java.io.FileNotFoundException).

The file definitely exists in my Assets folder and has the correct name. I've found a couple of similar questions on here and have tried various things including refreshing the project, cleaning the project, using "levels.txt" instead of "assets/levels.txt" but I keep getting this error.

Any ideas why?

6
  • The path is not what you think it is. Have you tried debugging? Commented Jul 14, 2013 at 9:21
  • Are these resources supplied by you or the user? If by you, access them as an embedded-resource by URL. If the user, offer them a JFileChooser. Commented Jul 14, 2013 at 9:22
  • Maybe you should know what current work directory is at first. System.out.println("Working Directory = " + System.getProperty("user.dir")); Commented Jul 14, 2013 at 9:35
  • @AndrewThompson This is s a file that is supplied by me. It contains level layout information for my game which is exported from a little level creator / editor that I wrote using swing Commented Jul 14, 2013 at 9:39
  • 1
    And once again a downvote with no comment as to why. This helps nobody. Commented Jul 14, 2013 at 9:57

2 Answers 2

7

Because you're dealing with outside the package, getResource() will be the best solution for your problem:

URL url = getClass().getResource("/assets/levels.txt"); File f = new File(url.toURI()); //.... 

Or you can directly get the input stream using getResourceAsStream() method :

InputStream is= getClass().getResourceAsStream("/assets/levels.txt"); isr = new InputStreamReader(is); 

It's better since you don't have to use FileInputStream.

Note that URISyntaxException must be caught with FileNotFoundException or declared to be thrown.

Sign up to request clarification or add additional context in comments.

1 Comment

There is little point in converting the URL to a File. Most methods/constructors that will accept a File will also accept an URL.
6

In an Android project, the right way to read the content of asset files is by using the AssetManager. Asset files are the files you put in the assets/ folder of your Android project. This is mentioned briefly in the sidebar on the Accessing Resources page in the Android docs.

In particular, you can open the file assets/levels.txt and create a BufferedReader like this:

InputStream stream = context.getAssets().open("levels.txt"); BufferedReader reader = new BufferedReader( new InputStreamReader(stream)); 

Notice that the argument of the open call is simply levels.txt, not assets/levels.txt.

For more details see the full docs of AssetManager.

5 Comments

Hi @Jabos yes this is an Android project. Basically what I've done is, write a level editor in Java/Swing. This outputs level layout information to a text file which I put in my assets folder and then run the app on my tablet / handset. Is this the correct way of doing this? If I use inputStream as you've indicated in the last part of your answer, it doesn't seem to work how would I integrate this line into my code? Do I use it instead of file levelFile = new file? Thanks! :-)
@Zippy see my updated answer. This is the right way to read asset file content in an Android project.
Thank you for the answer @janos, for some reason I had trouble with this, so I used Azad's similar answer. Thanks :-)
@Zippy I amended my answer to be really specific. This is the right way of doing this, and I've never had a problem with it. If you have trouble with this method, then I you must be doing something wrong. You should fix that, rather than choosing a less clean solution.
This should be the accepted answer, as it is the proper Android way of reading local files.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.