现在在做一个手机的应用,其中的一个功能就是向反指定的手机号码发送短信,我把这个手机号设计成了让用户可以修改的,方法是用了一个properties文件,这个文件放在了/res/raw目录下,写了一个JAVA工具类(就是读取properties的工具类),用来读取和保存配置文件中的内容,现在问题来了,用户设置好号码后本次确实是修改成功了,可是重新启动程序时又变成了默认的号码了,推测结果可能是修改后根本没有更新properties这个配置文件,已经试过好多次了都是这个样子,不知道怎么办呢。这是我的第一个安卓项目,功能十分简单,就是这个问题困扰住了,没有想到解决办法,哪位大侠能帮忙解决一下啊!!!

解决方案 »

  1.   

    raw/assets里的配置文件不能修改的建议放在/data/data/包名/files/目录下openFileInput(name);
    openFileOutput(name, mode);通过这两个方法读写文件
      

  2.   

    就是阿。放在data下面多安全啊。也不会因为重新格机器被覆盖掉。
      

  3.   

    一般我们的配置都通过Preference类保存到手机上,raw可能只是只读的,另外即使允许写入,也至少需要权限的。建议单步调试确定是否真的写入成功,你可以写入后立即读取看看。
      

  4.   

    前面可能没有说清楚   现在贴上代码来看看啊:package com.zyyu;import java.io.FileInputStream;
    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();
    }
    }}
    上面这个是我帖子中提到的工具类,看看这样可以吗?
      

  5.   


    不知道放在data目录下怎么弄呢?大侠给能给个简单的示例吗?
      

  6.   

    不知道放在data目录下怎么弄的,大侠能给个简单的示例或者说明吗?
      

  7.   


    大概是说放到sqlite数据库里把