日期:2014-05-18  浏览次数:20609 次

关于java properties文件读写的问题
有两个文件a.properties和b.properties 
现在我想把a里面含有而b里面没有的东东拷贝到b文件里(仅指定key部分,value无视)
请问这个类应该怎样写?
谢谢!


------解决方案--------------------
可以取得propertyNames() 所有的key嘛,然后就做基本的遍历去比较
------解决方案--------------------
Java code


    public void over(File src,File dest) throws IOException{
        Properties srcP= new Properties();
        InputStream in = new FileInputStream(src);
        srcP.load(in);
        in.close();
        
        Properties destP = new Properties();
        in = new FileInputStream(dest);
        destP.load(in);
        in.close();
        
        Set destKeys = destP.keySet();
        Set srcKeys = srcP.keySet();
        
        for(Object key:srcKeys){
            if(!destKeys.contains(key)){
                //不包含
                destP.put(key, srcP.get(key));
            }
        }
        
        OutputStream out = new FileOutputStream(dest);
        destP.store(out, "By shadao");
        out.close();
    }

------解决方案--------------------
楼上的正解!
可以用!destP.contains(key) 方法来判断是否包含key