java - Can't write text into .properties file with FileOutputStream on android -


this part of code have in settingsactivity.java file:

public class settingsactivity extends activity {      thread refresh = new thread() {         @override         public void run() {             while (!thread.currentthread().isinterrupted()) {                 try {                     config.writefile();                     thread.sleep(1000);                 } catch (interruptedexception e) {                     thread.currentthread().interrupt();                 }             }         }     };      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_settings);          if (new file(mainactivity.configpath).exists()) {             config.loadfile();         } else {             config.writefile();         }          refresh.start();     } } 

and in config.java file:

public class config {      public static void writefile() {         try {             properties properties = new properties();             properties.setproperty("startonboot", string.valueof(mainactivity.startonboot));              //startonboot boolean in mainactivity.java file              file file = new file("config/app.properties");             file.setwritable(true);             fileoutputstream fileout = new fileoutputstream(file);             properties.store(fileout, "favorite things");             fileout.close();         } catch (filenotfoundexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }     }      public static void loadfile() {         try {             properties properties = new properties();             fileinputstream filein = new fileinputstream(new file("config/app.properties"));             properties.load(filein);             mainactivity.startonboot = boolean.valueof(properties.getproperty("startonboot"));             filein.close();             system.out.println(properties.getproperty("startonboot"));         } catch (exception e) {             e.printstacktrace();         }     } } 

this project explorer window looks like

this shown in logfile logcat:

07-09 16:16:32.416: w/system.err(621): @ java.io.fileoutputstream.(fileoutputstream.java:94)

and .properties file still remains empty.

any ideas how make work?

ps:

-api 10

-target sdk version 22

-i didn't add permissions androidmanifest.xml file

for handling properties in android, use class:

public class mypropertiesholder {    public static int mode_create = 0;    public static int mode_update = 1;    private context context;    private string filename;    private properties properties;    public mypropertiesholder(context context, string filename, int mode) throws ioexception {           this.context = context;           this.filename = filename;           this.properties = new properties();           if(mode != mode_create)  {               fileinputstream inputstream = context.openfileinput(filename);               properties.load(inputstream);               inputstream.close();           }    }    public string getproperty(string key){        return (string) properties.get(key);    }    public void setproperty(string key, string value) {        properties.setproperty(key, value);    }    public void commit() throws ioexception {        fileoutputstream outputstream = context.openfileoutput(filename, context.mode_private);        properties.store(outputstream, "");        outputstream.close();    } } 

this how use above class (assuming in activity):

creating new properties file:

mypropertiesholder propholder = new mypropertiesholder(this, "test.properties", mypropertiesholder.mode_create); propholder.setproperty("test1", "test1"); propholder.setproperty("test2", "test2"); propholder.commit(); 

updating existing properties file:

mypropertiesholder propholder2 = new mypropertiesholder(this, "test.properties", mypropertiesholder.mode_update); string test1 = propholder2.getproperty("test1"); string test2 = propholder2.getproperty("test2"); propholder2.setproperty("test3", "test3"); propholder2.setproperty("test4", "test4"); propholder2.commit(); 

note: won't see file in assets directory.


Comments