[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集合里的值时,不会保存在文件里。为什么放在下边才有效?
//将一个文件中的键值对信息存入到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集合里的值时,不会保存在文件里。为什么放在下边才有效?
解决方案 »
- 如何将jfreechart生成的图片弄的更漂亮
- HTML标签底层的实现原理、机制
- 如何遍历MAP类型的里的内容?
- 如何得到2005年第20周的开始日期和截至日期?
- JNI入门,各位好人给些UNIX下的简单的例子吧
- 问一个applet的问题
- BufferedReader br =new BufferedReader( new FileReader( filename ) ),这里filename难道一定要写绝对路径吗?
- 一个继承的问题,加了构造函数后就不能编译通过
- 有关ClassPath 的问题
- jdk里面都能运行什么程序?
- 如何根据给定的一个日期得到离它最近的某一天?
- 方法中的内部类访问方法中的局部变量的问题
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
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");放到======这行就不行了,是为什么?
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已经把内容加载到了内存,后覆盖。
FileOutputStream fos = new FileOutputStream(String fileName);本身就会覆盖文件。
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);
}
}
会检查是否文件存在,在决定是否创建文件。
小弟大概是了解了,
因为FileOutputStream fos = new FileOutputStream(String fileName);本身就会覆盖文件。
所以把这句话写在======这行 会覆盖里面的数据,后面调用p.setProperty("stu1","555");
所以内存当中只有("stu1","555")这个键值对,当调用 p.store(fos, "");只是就将("stu1","555")写入到输出流中,所以最后的文件里只有("stu1","555")。