The file is private for the application, so isn't accessible for other apps or the user.
To write some string data to the device:
Using java Syntax Highlighting
- // Save settings
- public void WriteSettings(Context context, String data){
- FileOutputStream fOut = null;
- OutputStreamWriter osw = null;
- try{
- fOut = openFileOutput("settings.dat",MODE_PRIVATE);
- osw = new OutputStreamWriter(fOut);
- osw.write(data);
- osw.flush();
- Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
- }
- catch (Exception e) {
- e.printStackTrace();
- Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
- }
- finally {
- try {
- osw.close();
- fOut.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
to read the same settings:
Using java Syntax Highlighting
- // Read settings
- public String ReadSettings(Context context){
- FileInputStream fIn = null;
- InputStreamReader isr = null;
- char[] inputBuffer = new char[255];
- String data = null;
- try{
- fIn = openFileInput("settings.dat");
- isr = new InputStreamReader(fIn);
- isr.read(inputBuffer);
- data = new String(inputBuffer);
- Toast.makeText(context, "Settings read",Toast.LENGTH_SHORT).show();
- }
- catch (Exception e) {
- e.printStackTrace();
- Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show();
- }
- finally {
- try {
- isr.close();
- fIn.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return data;
- }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
How to use the write method:
Using java Syntax Highlighting
- WriteSettings(this,"setting0, setting1, setting2");
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
How to read the settings (put in a array for easier use):
Using java Syntax Highlighting
- String data[] = ReadSettings(this).split(",");
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Cheers




