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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Adam
  • 203
  • 1
  • 14
  • your path for asset folder is wrong – Vivek Mishra Dec 27 '18 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: https://stackoverflow.com/questions/9544737/read-file-from-assets – Robert Dec 27 '18 at 12:27
  • 1
    Possible duplicate of [read file from assets](https://stackoverflow.com/questions/9544737/read-file-from-assets) – lelloman Dec 27 '18 at 12:29
  • @Robert Would what you sent still work for a JSONReader using FileReader? – Adam Dec 27 '18 at 12:31
  • @Adam just don't use `FileReader`, just use the reader instead as shown in Daniel's answer. – Robert Dec 27 '18 at 13:24

2 Answers2

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.

Daniel B.
  • 2,491
  • 2
  • 12
  • 23
  • Just one minor improvement to your answer: use `StandardCharsets.UTF_8` instead of `"UTF-8". This saves you one unnecessary catch block. – Robert Dec 27 '18 at 13:03
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
    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.** – Daniel B. Dec 27 '18 at 12:44