Bluemarble
Bluemarble

Reputation: 2059

Android - How to write in a text file stored in project folders?

I want to create an app where users can save some preferences to a config.txt file. next time when the app is loaded, the configs will be read from the text file and will be applied in the UI.

I tried to put the config.txt file in \res\raw folder but came to know that you can not write a text file if it is stored inside Raw. Which folder can I use to write the file to?

private final static String STORETEXT="config.txt";
OutputStreamWriter out= new OutputStreamWriter(openFileOutput(STORETEXT,0)); 
out.write(txtEditor.getText().toString()); 
out.close();

also I have two more questions:

  1. How do I specify/refer to a project directory path in my STORETEXT variable?

  2. If in future I need to provide an update to my app, then will the new apk's config.txt overwrite the old config.txt? (causing users to loose all previous settings?) How can I prevent that?

Upvotes: 0

Views: 1922

Answers (2)

hp ling
hp ling

Reputation: 1

use sharedPreference or FileOutputStream outStream =this.openFileOutput("*.txt",Context.MODE_PRIVATE);

both saved path:/data/data/(package name)/files

Upvotes: -1

Jejefcgb
Jejefcgb

Reputation: 390

The path to your app folder is this one :

getFilesDir().getAbsolutePath() + "filename"

If you update your apk, this file won't be deleted. It will only be removed if you uninstall the app, not i you reinstall a new version.

However, SharedPreferences might be a better way to store the user preferences.

Upvotes: 2

Related Questions