使用properties类来读入读出文件,如果只有读入操作会自动清空保存的properties.pro,无奈下只有用读入之后再写入的折衷方法。请问是否有方法可以设置,只有读入操作也不会清空.pro文件?
这个是源文件package test;import java.io.*;
import java.util.Properties;public class PropertyTest {
private Properties pro;
private InputStream is;
private File file;
private OutputStream os; public PropertyTest() {
file = new File("d:\\properties.pro");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
pro = new Properties();
// 设置pro输入流
try {
is = new FileInputStream(file);
pro.load(is);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 设置pro输出流
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // 读入该属性
public long read(String key) {
long data = 0;

try {
data = Long.parseLong(pro.getProperty(key));
//若只有读取会清空.pro文件,只好如此折衷
pro.store(os, "comments");
} catch (Exception e) {
data = -1;
e.printStackTrace();
}
return data;
} // 写出该属性并store进文件
public void write(long value, String key) {
pro.setProperty(key, Long.toString(value));
try {
pro.store(os, "comments");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public static void main(String[] args) {
PropertyTest pt = new PropertyTest();
// pt.write(1234, "index");
// pt.write(1234, "second");
System.out.println(pt.read("index"));
//                System.out.println(pt.read("index"));
// System.out.println(pt.read("second"));
}
}

解决方案 »

  1.   

    使用properties类来读入读出文件,如果只有读入操作会自动清空保存的properties.pro。怎么会自动清空?
      

  2.   

    你可以试一下,先使用pt.write(1234, "index")写入,然后调用read()方法,注意要将折衷的
                //若只有读取会清空.pro文件,只好如此折衷
                pro.store(os, "comments");
    这一段删掉
      

  3.   


    我也写过properties文件,写进去就进去了。没有出现楼主所说的自动清空。
      

  4.   


    我前段时间才写的代码,楼主参考下。package com.qq.regist;import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Properties;public class UserInformation { public Properties userInfomation; //注册一个用户
    public void insert(String userName, String userPassword) {
    try {
    userInfomation = new Properties();
    InputStream is;
    OutputStream os;
    is = new FileInputStream("d:/userInfo.properties");
    os = new FileOutputStream("d:/userInfo.properties", true);
    userInfomation.load(is);
    // 将用户名和密码存储到内存中
    userInfomation.setProperty(userName, userPassword);
    // 将用户名和密码保存到文件中
    userInfomation.store(os, null); } catch (FileNotFoundException e1) {
    System.out.println("文件userInfo.properties没有找到 ");
    } catch (IOException e) {
    System.out.println("写 userInfo.properties 出错");
    }
    } //判断此用户名是否存在
    public boolean isExist(String userName) {
    try {
    userInfomation = new Properties();
    InputStream is;
    is = new FileInputStream("d:/userInfo.properties");
    userInfomation.load(is);
    if (userInfomation.containsKey(userName)) {
    return true;
    } } catch (FileNotFoundException e1) {
    System.out.println("文件userInfo.properties没有找到 ");
    } catch (IOException e) {
    System.out.println("写 userInfo.properties 出错");
    }
    return false;
    }}
      

  5.   

    算了,我们头说用pro不合适,还是用RandomAccessFile吧,谢谢huxiweng
      

  6.   

    哦,解决了,这是像6楼说的,逻辑混乱,呵呵,问题是每次初始化
    // 设置pro输出流
    try {
    os = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    这里会把原来文件清空郁闷