简体   繁体   English

无法在Android上使用FileOutputStream将文本写入.properties文件

[英]Can't write text into .properties file with FileOutputStream on android

This is a part of the code I have in my SettingsActivity.java file: 这是我在SettingsActivity.java文件中拥有的代码的一部分:

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: 这是在我的Config.java文件中:

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 : 这是logcat日志文件中显示的内容:

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

And the .properties file still remains empty. 并且.properties文件仍然保持为空。

Any ideas how to make it work? 有什么想法使它起作用吗?

PS: PS:

-API 10 -API 10

-Target SDK Version 22 -Target SDK版本22

-I didn't add any permissions to the AndroidManifest.xml file -我没有向AndroidManifest.xml文件添加任何权限

For handling properties in Android, use this class: 要在Android中处理属性,请使用此类:

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 ): 这是使用上面的类的方法(假设您处于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. 注意:您不会在Assets目录中看到此文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM