现在在做一个手机的应用,其中的一个功能就是向反指定的手机号码发送短信,我把这个手机号设计成了让用户可以修改的,方法是用了一个properties文件,这个文件放在了/res/raw目录下,写了一个JAVA工具类(就是读取properties的工具类),用来读取和保存配置文件中的内容,现在问题来了,用户设置好号码后本次确实是修改成功了,可是重新启动程序时又变成了默认的号码了,推测结果可能是修改后根本没有更新properties这个配置文件,已经试过好多次了都是这个样子,不知道怎么办呢。这是我的第一个安卓项目,功能十分简单,就是这个问题困扰住了,没有想到解决办法,哪位大侠能帮忙解决一下啊!!!
解决方案 »
- 刚学习android开发几天,在做打老鼠时遇到的一些问题
- 求手机游戏的赛道绘制方法
- mapview定位问题,会的朋友帮忙分析下。谢谢!
- android socket
- Android VIRTUAL_HARD_KEY
- 想在android 3.0以下版本实现search widget样式的搜索框,有什么办法没?
- Android google map 开发 ,求帮助!!!!!!
- android 调试问题,ART调试,ART模式,大家来说说
- 在一个activity里面怎么监听别的xml的控件的事件
- Android5.1怎么用代码实现打开数据连接。。。在线急等
- 使用sdl遇到的问题。请高手来。
- Android挂机键键位检测不到啊,怎么回事?
openFileOutput(name, mode);通过这两个方法读写文件
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;public class Configuration {
private static Properties pro;
private static FileInputStream inputFile;
private static FileOutputStream outputFile; private static final Configuration m_instance=new Configuration();
private Configuration() {
pro = new Properties();
try {
// 装载文件
pro.load(Configuration.class.getResourceAsStream("/res/raw/socket.properties"));
} catch (FileNotFoundException e) {
System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
e.printStackTrace();
} catch (IOException e) {
System.out.println("装载文件--->失败!");
e.printStackTrace();
}
}
public static Configuration getInstanace(){
return m_instance;
}
private Configuration(InputStream filePath) {
pro = new Properties();
System.out.println(filePath);
try {
// 装载文件
pro.load(filePath);
inputFile.close();
} catch (FileNotFoundException e) {
System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
e.printStackTrace();
} catch (IOException e) {
System.out.println("装载文件--->失败!");
e.printStackTrace();
}
}
/**
* 初始化Configuration类
*
* @param filePath
* 要读取的配置文件的路径+名称
*/
private Configuration(String filePath) {
pro = new Properties();
System.out.println(filePath);
try {
// 读取属性文件
inputFile = new FileInputStream(filePath);
// 装载文件
pro.load(inputFile);
inputFile.close();
} catch (FileNotFoundException e) {
System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
e.printStackTrace();
} catch (IOException e) {
System.out.println("装载文件--->失败!");
e.printStackTrace();
}
} /**
* <p>
* 得到key值
* </p>
*
* @param key
* 取得其值的键
* @return 取得键值
*/
public String getValue(String key) {
if (pro.containsKey(key)) {
String value = pro.getProperty(key);
return value;
} else {
return "";
}
} /**
* <p>
* 得到key值
* </p>
*
* @param filePath
* properties文件的路径+文件名
* @param key
* 取得其值的键
* @return 取得键值
*/
public String getValue(String filePath, String key) {
try {
String value = "";
inputFile = new FileInputStream(filePath);
pro.load(inputFile);
inputFile.close();
if (pro.contains(key)) {
value = pro.getProperty(key);
return value;
} else {
return "";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
}
} /**
* <p>
* 清除properties文件中所有的key和其值
* </p>
*/
public void clear() {
pro.clear();
} /**
* <p>
* 改变或添加一个key的值
* </p>
* 当key存在于properties文件中时该key的值被value所代替, 当key不存在时,该key的值是value
*
* @param key
* 要存入的键
* @param value
* 要存入的值
*/
public void setValue(String key, String value) {
pro.setProperty(key, value);
} /**
* <p>
* 将更改后的文件数据存入指定的文件中,该文件可以事先不存在
* </p>
*
* @param fileName
* 文件路径+文件名称
* @param description
* 对该文件的描述
*/
public void saveFile() {
try {
outputFile = new FileOutputStream("/res/raw/socket.properties");
pro.store(outputFile, null);
outputFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}}
上面这个是我帖子中提到的工具类,看看这样可以吗?
不知道放在data目录下怎么弄呢?大侠给能给个简单的示例吗?
大概是说放到sqlite数据库里把