黑马程序员技术交流社区

标题: 关于Properties的顺序问题 [打印本页]

作者: thoris    时间: 2015-5-19 22:44
标题: 关于Properties的顺序问题
看到第20天 13课的 properties内容
主要代码如下
  1. public class H_11Properties {

  2.         public static void main(String[] args) throws IOException {

  3.                 //setAndGet();
  4.                 method_1();
  5.                 loadDemo();
  6.                
  7.         }
  8.         public static void loadDemo() throws IOException
  9.         {
  10.                 Properties prop=new Properties();
  11.                 FileInputStream fis=new FileInputStream("info.txt");
  12.                 //将流中的数据加载进集合
  13.                 prop.load(fis);
  14.                 sop(prop);
  15.                 prop.list(System.out);
  16.         }
  17.         public static void method_1() throws IOException
  18.         {
  19.                 BufferedReader bufr=new BufferedReader(new FileReader("info.txt"));
  20.                 String line=null;
  21.                 Properties prop=new Properties();
  22.                 while((line=bufr.readLine())!=null)
  23.                 {
  24.                         //sop(line);
  25.                         String[] arr=line.split("=");
  26.                         //sop(arr[0]+"  "+arr[1]);
  27.                         prop.setProperty(arr[0], arr[1]);
  28.                         //顺序按照哈希表
  29.                 }
  30.                 bufr.close();
  31.                 sop(prop);
  32.                        
  33.         }
  34. 后面还有sop那个
复制代码


info.txt文档里面的内容是随便打的,具体如下
zhangsan=34
zaijian=hha
李四=hah
duqu=78
hett=http


主函数调用了两个方法:
                method_1();
                loadDemo();//这里面有两个打印动作
结果是这样:
{duqu=78, hett=http, zaijian=hha, zhangsan=34, 李四=hah}
{duqu=78, ????=hah, hett=http, zaijian=hha, zhangsan=34}
-- listing properties --
duqu=78
????=hah
zaijian=hha
hett=http
zhangsan=34


顺序和原文本不同还可以理解,但是为什么三种打印方式的输出顺序都不一样?
作者: thoris    时间: 2015-5-19 22:50
其中的问号是因为用字节流读了字符的缘故,改用转换流的话,
method_1的结果和loadDemo的sop(prop)是一样的
但是和prop.list(System.in) 还是不一样




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