0

Everytime I run the code I get java.io.FileNotFoundException: search.json: open failed: EROFS (Read-only file system)

here's the code

private static void writeJSearchKey(JSONObject obj) throws IOException {
        //write the searchKey value to the json file
        FileWriter file = new FileWriter("search.json");
        file.write(obj.toString());
        file.flush();
        file.close();
    }

and here's where the JSONObejct comes from (just for the sake of Context)

private static JSONObject writeJsonFile(String key) throws JSONException {
        //get searchInput string to JSON file
        JSONObject o = new JSONObject();
        o.put("searchKey", key);

        JSONObject newO= new JSONObject();
        newO.put("searchEngine", o);

        String oString= newO.toString();
        Log.d(TAG, oString);
        return newO;
    }
  • Have you checked to see that the file exists (in your storage)? [This](https://stackoverflow.com/questions/15711098/trying-to-create-a-file-in-android-open-failed-erofs-read-only-file-system) should help. – tomerpacific Jun 06 '20 at 18:41
  • Okay, so you're trying to write to a file called `search.json` - but in which directory? I suspect that "the current working directory" isn't writable on Android. – Jon Skeet Jun 06 '20 at 18:41
  • search.json should be in your root folder of the project directory. – ProGamer Jun 06 '20 at 18:41
  • The json file is located in an Assest folder but when I right clicked to copy the root file path it gave me search.json – Shahar Brandman Jun 06 '20 at 18:46
  • @tomerpacific I have tired moving it to the root folder and It still didn't work, altho I will look in the link you sent – Shahar Brandman Jun 06 '20 at 18:49
  • @ProGamer you mean the com.example.app folder? – Shahar Brandman Jun 06 '20 at 19:08

0 Answers0