0

This is a part of the code I have in my 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 this is in my Config.java file:

public class Config {

    public static void writeFile() {
        try {
            Properties properties = new Properties();
            properties.setProperty("StartOnBoot", String.valueOf(MainActivity.StartOnBoot));

            //StartOnBoot is a boolean in the 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 is what my Project explorer window looks like

This is what is shown in a logfile from logcat:

07-09 16:16:32.416: W/System.err(621): at java.io.FileOutputStream.(FileOutputStream.java:94)

And the .properties file still remains empty.

Any ideas how to make it work?

PS:

-API 10

-Target SDK Version 22

-I didn't add any permissions to the AndroidManifest.xml file

Tom Lenc
  • 765
  • 1
  • 17
  • 41

1 Answers1

0

For handling properties in Android, use this 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 is how you use the above class (assuming you are in an Activity):

Creating a 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: You won't see this file in assets directory.

Balkrishna Rawool
  • 1,865
  • 3
  • 21
  • 35
  • `flush()` before `close()` is redundant, and `FileOutputStream.flush()` doesn't do anything anyway. – user207421 Jul 09 '15 at 23:22
  • @Balkrishna Rawool ok, this seems promising but how do I check if the file already exists? – Tom Lenc Jul 10 '15 at 01:20
  • @EJP: You are right. I added that while I was debugging something. But left it there. Now it is removed. – Balkrishna Rawool Jul 10 '15 at 08:28
  • @TomLenc: You can easily do this in your app. When the app is first opened, create the file. On all subsequent attempts, update the file. – Balkrishna Rawool Jul 10 '15 at 08:29
  • @Balkrishna Rawool but where does the file create and how do I get it's path? Please add reading of the file.. into the propertiesHolder... – Tom Lenc Jul 10 '15 at 09:57
  • @Balkrishna Rawool I think I got a solution.. Please make a chat room so we can talk about it – Tom Lenc Jul 10 '15 at 10:09
  • Well, this is what I have for reading actually... `ConfigHandler propHolder = new ConfigHandler(ctx, "app.properties", ConfigHandler.MODE_READ); MainActivity.StartOnBoot = Boolean.valueOf(propHolder.getProperty("StartOnBoot"));` And I edited the propertiesHolder file.. now in the commint() method checks if file laready exists, if does, id deletes it right before it creates the file. – Tom Lenc Jul 10 '15 at 10:16
  • I found a solution and it works. It was working the whole time.. I had wrongly positioned parts of code in Threads that were ruinig it.. – Tom Lenc Jul 10 '15 at 13:42
  • Glad that you found a solution. – Balkrishna Rawool Jul 11 '15 at 07:49