Use PrintWriter to recreate a text file in /res/raw/ with android?
-
26-12-2019 - |
Question
I am creating an app where the configuration file needs to be re-written on startup and would like to use PrintWriter to create it. The problem is PrintWriter takes a file as a parameter. is there any way to reference a text document in /res/raw/ as File object in order to fit the parameters for PrintWriter?
I don't know if this will help but here is some code to better explain my question: (its the basic idea but doesnt work...)
PrintWriter writer = new PrintWriter(new File("res/raw/config.txt"));
/*(I need to acces my config file... here ) */
Solution
You can't write to the res/raw directory because it is part of your apk file. You will need to either save the configuration a preference file using PreferenceManager.getDefaultSharedPreferences(Context context)
or write a file to your app private directory. You can create a file like this:
PrintWriter writer = new PrintWriter(context.openFileOutput("config.txt", MODE_PRIVATE));