[code=Java][/
//将一个文件中的键值对信息存入到Properties集合中,并且将集合中修改好后的数据再次存入文件中
import java.util.*;
import java.io.*;
class PropertiesDemo1
{
public static void main(String[] args) throws IOException
{
FileInputStream fis=new FileInputStream("e:"+File.separator+"11.txt");//创建一个输入刘传入load()
=================================================================================
Properties p=new Properties();
p.load(fis);
p.setProperty("stu1","111");//修改内存中的键值对信息,此操作并没有修改源文件
FileOutputStream fos=new FileOutputStream("e:"+File.separator+"11.txt");
p.store(fos,"");//将内存中修改好的内容存入源文件,第二个参数是注释的意思
p.list(System.out);
fis.close();
fos.close();
}
}
]
小弟想问的是为什么将FileOutputStream fos=new FileOutputStream("e:"+File.separator+"11.txt");
这句话放到=============这行的时候,我修改Properties集合里的值时,不会保存在文件里。为什么放在下边才有效?

解决方案 »

  1.   

    貌似结果跟楼主的不一样。
    public class PropertiesDemo1 {
    public static void main(String[] args) throws IOException {
    FileInputStream fis = new FileInputStream("test.txt");// 创建一个输入刘传入load()
    // =================================================================================
    FileOutputStream fos = new FileOutputStream("test.txt");
    Properties p = new Properties();
    p.load(fis);
    p.setProperty("stu1", "111");// 修改内存中的键值对信息,此操作并没有修改源文件
    p.store(fos, "");// 将内存中修改好的内容存入源文件,第二个参数是注释的意思
    p.list(System.out);
    fis.close();
    fos.close();
    }
    }
    #
    #Wed May 09 19:29:57 CST 2012
    stu1=111
      

  2.   

    [//将一个文件中的键值对信息存入到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","555");
    FileOutputStream fos=new FileOutputStream("e:"+File.separator+"11.txt");
    p.store(fos,"");
    System.out.println(p);
    }
    }][/code]
    11.txt文件中  本来就有 
    stu1=111
    stu2=222
    stu3=333
    我现在是想改成stu1=555
    上面的代码是可以实现的,但是
    FileOutputStream fos=new FileOutputStream("e:"+File.separator+"11.txt");放到======这行就不行了,是为什么?
      

  3.   


    public class PropertiesDemo1 {
    public static void main(String[] args) throws IOException {
    Properties p = new Properties();
    FileInputStream fis = new FileInputStream("test.txt");// 创建一个输入刘传入load()
    // ==================================================================
    FileOutputStream fos = new FileOutputStream("test.txt");
    // p.load(fis);
    // System.out.println(p);
    // p.setProperty("str3", "333");
    // p.store(fos, "");
    // System.out.println(p);
    fis.close();
    // fos.flush();
    fos.close();
    }
    }FileInputStream fis = new FileInputStream("test.txt");// 创建一个输入刘传入load()
    FileOutputStream fos = new FileOutputStream("test.txt");
    文件输入流输出流同时开着,fos新建一个文件把原文件覆盖。所以p.load();打印也是空的。
    前面不一样的原因是,p已经把内容加载到了内存,后覆盖。
      

  4.   

    说错了。
    FileOutputStream fos = new FileOutputStream(String fileName);本身就会覆盖文件。
      

  5.   

        public FileOutputStream(String name, boolean append)
            throws FileNotFoundException
        {
            this(name != null ? new File(name) : null, append);
        }
    源码直接 new File(name)了
    不想要覆盖的话用下面这种格式
    public FileOutputStream(File file, boolean append)
            throws FileNotFoundException
        {
            String name = (file != null ? file.getPath() : null);
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkWrite(name);
    }
            if (name == null) {
                throw new NullPointerException();
            }
    fd = new FileDescriptor();
            fd.incrementAndGetUseCount();
            this.append = append;
    if (append) {
        openAppend(name);
    } else {
        open(name);
    }
        }
    会检查是否文件存在,在决定是否创建文件。
      

  6.   

    这又没事,有啥好道歉的,解决问题就行了,小弟感激都来不及呢。
    小弟大概是了解了,
    因为FileOutputStream fos = new FileOutputStream(String fileName);本身就会覆盖文件。
    所以把这句话写在======这行  会覆盖里面的数据,后面调用p.setProperty("stu1","555");
    所以内存当中只有("stu1","555")这个键值对,当调用 p.store(fos, "");只是就将("stu1","555")写入到输出流中,所以最后的文件里只有("stu1","555")。