Search code examples
javaandroidfilewriterandroid-file

why new FileWriter(...) return /data/data/com.myapp.client/files/myfile.json (No such file or directory)


Why when I do :

FileWriter fw = new FileWriter("/data/data/com.myapp.client/files/myfile.json", true/*append*/); 

on some very few device (one most of them it's work well) I receive : /data/data/com.myapp.client/files/myfile.json (No such file or directory). How can I avoid this error ?


Solution

  • Don't hardcode the path, use Context.getFilesDir instead

    File file = new File(context.getFilesDir(), "file.json")
    

    See this for reference.