黑马程序员技术交流社区

标题: Propterties存储从字符流读取的键和值的问题 [打印本页]

作者: 郑传庆    时间: 2012-6-10 10:11
标题: Propterties存储从字符流读取的键和值的问题
我知道Propterties里面提供有load()是用来读取字符流的键和值的,可我下面没用这个方法,只是将键值对切割然后存在Propterties里的。

public static void main(String[] args) throws Exception {
                PropterValues();
        }
        /**
         * 用流来读取文件的键值对
         * @throws IOException
         */
        public static void PropterValues() throws IOException{

                FileReader in = new FileReader("E:\\myuser\\JavaFileList.txt");
                BufferedReader buff = new BufferedReader(in);
                Properties p = new Properties();
                String line =null;
                while((line = buff.readLine())!=null){
                        String arr[] = line.split("==");//切割字符串
                        p.setProperty(arr[0], arr[1]);//为什么这里会报错?
                }
                buff.close();
                System.out.println(p);
        }
作者: 潘东升    时间: 2012-6-10 10:29
你怎么用两个“=”切割?是文件内容就是这样?
作者: 赵兵锋    时间: 2012-6-10 10:37
  1. while((line = buff.readLine())!=null){
  2.                 String arr[] = line.split("=");//这里一定是一个等号,因为这就是Properties文件的格式
  3.                 p.setProperty(arr[0], arr[1]);//若上面是两个等号,那对于"name=bee"这样的字符串是切不出两个来的,就一个arr[0]="name=bee",没有arr[1],所以会报数组下标越界
  4.         }
复制代码





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