zoukankan      html  css  js  c++  java
  • Properties类与流的相关方法,properties的键与值都应该是字符串

    java.util.Properties 继承于Hashtable ,来表示一个持久的属性集。它使用键值结构存储数据,每个键及其对应值都是一个字符串。该类也被许多Java类使用,比如获取系统属性时,System.getProperties 方法就是返回一个Properties对象。

    /* Student.txt文件内容:
     * 刘伊=18
     * 王含=20
     * 李风风=17
     * 刘方=16
     * 马红红=20
     * 丁磊=18
     * 方影=21
     * 姚华华=20
     */
    public class Demo04 {
        public static void main(String[] args) throws IOException {
            File file = new File("Student.txt");
            File newFile = new File("newstu.txt");
    
            Properties properties = new Properties();
            FileReader fr = new FileReader(file);
            properties.load(fr);
            Set<String> names = properties.stringPropertyNames();
            FileWriter fw = new FileWriter(newFile);
            for (String name : names) {
                if (name.equals("刘方")){
                    properties.put("刘方",18);
                }
            }
            properties.store(fw,null);
    
        }
    }
    Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
        at java.util.Properties.store0(Properties.java:834)
        at java.util.Properties.store(Properties.java:771)
        at homework.Demo04.main(Demo04.java:39)
    当properties的键与值有不为String类型时,调用store方法时会出现类型不能转换为String的异常。

    properties.put("刘方",18);应改为
    properties.put("刘方","18")
  • 相关阅读:
    grunt安装
    RequireJS实例分析
    Linux下解压rar文件
    windows(64位)下使用curl命令
    RequireJS学习资料汇总
    Linux下firefox安装flash player插件
    caj转pdf——包含下载链接
    《社会化营销:人人参与的营销力量》—— 读后总结
    《税的真相》—— 读后总结
    基于代理的数据库分库分表框架 Mycat实践
  • 原文地址:https://www.cnblogs.com/chenfx/p/13406888.html
Copyright © 2011-2022 走看看