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         ) */
Was it helpful?

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));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top