这是我的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 文件添加任何权限