0

I keep receiving the FileNotFoundException error when trying to load my file into a JSONReader using an InputStream within my onCreate method.

I've tested this code in a simple Java program and it seems to work fine and also reads the JSON file too. However, within Android Studio I keep receiving the FileNotFoundException.

Am I referencing the location of the file incorrectly?

This is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    List<String> linksList = new ArrayList<>();

    try {
        JsonReader reader = null;
        reader = new JsonReader(new FileReader("/assets/test.json"));
        reader.beginArray();

        while (reader.hasNext()) {

            String value = reader.nextString();
            linksList.add(value);
        }

        reader.endArray();
        reader.close();
    } catch (FileNotFoundException fnfe) {

        fnfe.printStackTrace();

    } catch (IOException ioe) {

        ioe.printStackTrace();
    }
}

Here is the Log -

12-27 11:56:11.342 20703-20703/com.adam.jsonreader W/System.err: java.io.FileNotFoundException: /assets/test.json (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at java.io.FileInputStream.<init>(FileInputStream.java:99)
12-27 11:56:11.343 20703-20703/com.adam.jsonreader W/System.err:     at java.io.FileReader.<init>(FileReader.java:58)
        at com.adamkhora.jsonreader.HomeActivity.onCreate(HomeActivity.java:43)

Here is my project structure:

project_structure

5
  • your path for asset folder is wrong Commented Dec 27, 2018 at 12:27
  • On a Linux system like Android the path "/assets/test.json" means that the file is located in the assets folder of the file-system-root not the data directory of the app. For correctly loading assets see this example: stackoverflow.com/questions/9544737/read-file-from-assets Commented Dec 27, 2018 at 12:27
  • 1
    Possible duplicate of read file from assets Commented Dec 27, 2018 at 12:29
  • @Robert Would what you sent still work for a JSONReader using FileReader? Commented Dec 27, 2018 at 12:31
  • @Adam just don't use FileReader, just use the reader instead as shown in Daniel's answer. Commented Dec 27, 2018 at 13:24

2 Answers 2

1

instead of new FileReader("/assets/test.json"),

use

new BufferedReader(new InputStreamReader(getAssets().open("test.json"), StandardCharsets.UTF_8)); 

You need to use the getAssets() method to use assets in android.

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

1 Comment

Just one minor improvement to your answer: use StandardCharsets.UTF_8 instead of `"UTF-8". This saves you one unnecessary catch block.
0

Just use below snippet by giving file name

 try {
        InputStream is = context.getAssets().open("test.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");

    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }

1 Comment

Do not use this snippet.. From the Java InputStream documentaion : Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.