黑马程序员技术交流社区

标题: 关于Properties的写入问题 [打印本页]

作者: 朱德帅    时间: 2013-4-3 14:21
标题: 关于Properties的写入问题
本帖最后由 朱德帅 于 2013-4-3 22:30 编辑
  1. public class PropertiesTest {
  2. public static void main(String[] args) throws FileNotFoundException {
  3. Properties prop = new Properties();
  4. prop.setProperty("name", "zhangsan");
  5. prop.setProperty("age", "19");
  6. prop.list(new PrintStream("src/a.properties"));
  7. }


  8. }
复制代码
本来我只是写入两个字段,但是结果却多了一个。然后再读取出来仍然是三个。

1.jpg (9.09 KB, 下载次数: 3)

1.jpg

作者: pthuakai    时间: 2013-4-3 14:35
这是我的代码,我的运行没问题啊
import java.io.*;
import java.util.*;

public class PropertiesDemo {

        public  static void method()throws IOException{
                BufferedReader bufr=new BufferedReader(new FileReader("byf.txt"));
                String line=null;
                Properties prop=new  Properties();
        }
        public static void setAndGet()
        {
                Properties prop=new Properties();
                prop.setProperty("zhangsan","20");
                prop.setProperty("lisi","28");
                String value=prop.getProperty("lisi");
                System.out.println(value);
        }
        public static void main(String[] args) {
                setAndGet();
        }
}
作者: 朱德帅    时间: 2013-4-3 14:46
pthuakai 发表于 2013-4-3 14:35
这是我的代码,我的运行没问题啊
import java.io.*;
import java.util.*;

我存入两个字段,但是里面多了一个字段。就是图里第一个,是多出来的。。
作者: pthuakai    时间: 2013-4-3 14:51
抱歉,我这里运行你的抛异常了
作者: pthuakai    时间: 2013-4-3 14:53
运行和你的一样了,我也晕了,我也求助
作者: 曹睿翔    时间: 2013-4-3 15:03
本帖最后由 曹睿翔 于 2013-4-3 15:11 编辑

这个问题不用纠结,list方法是用来调试用的请看源码
  1. /**
  2.      * Prints this property list out to the specified output stream.
  3.      * This method is useful for debugging.//这里
  4.      *
  5.      * @param   out   an output stream.
  6.      * @throws  ClassCastException if any key in this property list
  7.      *          is not a string.
  8.      */
  9.     public void list(PrintStream out) {
  10.         out.println("-- listing properties --");//还有这里,这么说吧,其实是list这个方法里你传入了PrintStream,在代码里,用打印流又写入了
  11. listing properties ,来说明你是在列出properties,真正用的时候你会去单纯打印他去,需要根据key获取value,你可以用迭代打印一下,肯定没多余的信息了
  12.         Hashtable h = new Hashtable();
  13.         enumerate(h);
  14.         for (Enumeration e = h.keys() ; e.hasMoreElements() ;) {
  15.             String key = (String)e.nextElement();
  16.             String val = (String)h.get(key);
  17.             if (val.length() > 40) {
  18.                 val = val.substring(0, 37) + "...";
  19.             }
  20.             out.println(key + "=" + val);
  21.         }
  22.     }
复制代码

作者: 吴林飞    时间: 2013-4-3 15:16
是这个吧:-- listing properties --
这个是属性列表自带的,并不是你存入的信息,你可以试着把自己存入的信息注释了,再list()一下,他还是会出现,而且你注意到,他在文件中不是以键值对的形式出现的。

作者: 袁梦希    时间: 2013-4-3 15:45
吴林飞 发表于 2013-4-3 15:16
是这个吧:-- listing properties --
这个是属性列表自带的,并不是你存入的信息,你可以试着把自己存入的 ...

楼上说的挺好  list里面传的是写入流   当调用list方法的时候 先在里面输出一段提示文字,告诉你这是list清单列表,   但是被eclipse识别为以键值对的形式了存入properties文件了,其实第一行就是一段文本。只要中间有“空格”或者“=”或者其他符号的properties文件,eclipse都会解析以键值对形式存在。所以不用考虑第一行的文字,当在控制台上输出prop变量的时候是真正的键值对。  

希望可以帮助楼主解决问题。

作者: 田磊阳    时间: 2013-4-3 22:19
如果问题未解决,请继续追问,如果没有问题了,请将帖子分类 改为“已解决”,谢谢




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2