当前位置: 代码迷 >> J2SE >> 关于Properties键值对存储解决方案
  详细解决方案

关于Properties键值对存储解决方案

热度:65   发布时间:2016-04-24 01:03:07.0
关于Properties键值对存储
Java code
//将一个文件中的键值对信息存入到Properties集合中,并且将集合中修改好后的数据再次存入文件中import java.util.*;import java.io.*;class PropertiesDemo1{    public static void main(String[] args) throws IOException    {            Properties p=new Properties();        FileInputStream fis=new FileInputStream("e:"+File.separator+"11.txt");//创建一个输入刘传入load()        ==================================================================        p.load(fis);        p.setProperty("stu1","111");        FileOutputStream fos=new FileOutputStream("e:"+File.separator+"11.txt");        p.store(fos,"");        System.out.println(p);    }}


11.txt文件中 本来就有  
stu1=111
stu2=222
stu3=333
我现在是想改成stu1=555
上面的代码是可以实现的,但是
FileOutputStream fos=new FileOutputStream("e:"+File.separator+"11.txt");放到======这行就不行了,是为什么?而且将这行放到======这行,11.txt中的数据也会清除。

------解决方案--------------------
FileOutputStream的构造函数如果只是传递一个File对象,那么默认是新创建一个file的,如果想不新建那么需要加一个boolean参数,表示追加,若这样的话又不符合你的初衷了
FileOutputStream(File file)//默认是false
FileOutputStream(File file, boolean flag)//若为true表示追加
  相关解决方案