0

これは、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();
    }
}

そして、これは私の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();
        }
    }
}

これは、私のプロジェクト エクスプローラーウィンドウがどの

これはlogcat からのログファイルに表示されるものです:

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

また、.propertiesファイルはまだ空のままです。

それを機能させる方法はありますか?

PS:

-API 10

-ターゲット SDK バージョン 22

- AndroidManifest.xml ファイルに権限を追加しませんでした

4

1 に答える 1

0

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();
   }
}

これは、上記のクラスを使用する方法です (あなたが にいると仮定しますActivity):

新しいプロパティ ファイルの作成:

MyPropertiesHolder propHolder = new MyPropertiesHolder(this, "test.properties", MyPropertiesHolder.MODE_CREATE);
propHolder.setProperty("test1", "test1");
propHolder.setProperty("test2", "test2");
propHolder.commit();

既存のプロパティ ファイルを更新しています:

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();

注: このファイルは assets ディレクトリには表示されません。

于 2015-07-09T22:28:42.650 に答える