• Properties的有序读写


    使用java.util.Properties提供的类,读取properties文件的时候,读出来的是乱序的

    如下边的情况

    import java.io.*;
    import java.util.Arrays;
    import java.util.Enumeration;
    import java.util.List;
    import java.util.Properties;
    
    public class PropertyDemo {
    
        public static List<String> old = Arrays.asList("自来水","纯净水", "矿泉水","山泉水" );
    
        public static void main(String[] args) {
            initType();
        }
    
        public static void initType() {
    
            String path = System.getProperty("user.dir").replaceAll("\\", "/");
            path = path + "/waterType.properties";
            File file = new File(path);
            Properties properties = new Properties();
            if (!file.exists()) {
                try {
                    FileOutputStream oFile = new FileOutputStream(path, true);
                    int i = 0;
                    int len = old.size();
                    for (; i < len; i++) {
                        properties.setProperty(String.valueOf(i + 1), old.get(i));
                    }
                    properties.store(oFile, "");
                    oFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
            try {
                InputStream in = new BufferedInputStream(new FileInputStream(path));
                properties.load(in);
                in.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
    
            //遍历
            Enumeration<?> e= properties.propertyNames();
            while (e.hasMoreElements()){
                String key = (String) e.nextElement();
                String value = properties.getProperty(key);
                System.out.println(key + "=" + value);
            }
    
        }
    }

    输出

    4=山泉水
    3=矿泉水
    2=纯净水
    1=自来水

    保存到文件的顺序也是如此

    那如果想是有序的,怎么办呢?

    自定义一个Properties 类

    import java.util.*;
    
    public class OrderedProperties extends Properties {
    
        private final LinkedHashSet<Object> keys = new LinkedHashSet<Object>();
    
        public Enumeration<Object> keys() {
            return Collections.<Object>enumeration(keys);
        }
    
        public Object put(Object key, Object value) {
            keys.add(key);
            return super.put(key, value);
        }
    
        public Set<Object> keySet() {
            return keys;
        }
    
        public Set<String> stringPropertyNames() {
            Set<String> set = new LinkedHashSet<String>();
            for (Object key : this.keys) {
                set.add((String) key);
            }
            return set;
        }
    
    }

    使用

    import java.io.*;
    import java.util.*;
    
    public class PropertyDemo {
    
        public static List<String> old = Arrays.asList("自来水","纯净水", "矿泉水","山泉水" );
    
        public static void main(String[] args) {
            initType();
        }
    
        public static void initType() {
    
            String path = System.getProperty("user.dir").replaceAll("\\", "/");
            path = path + "/waterType.properties";
            File file = new File(path);
            Properties properties = new OrderedProperties();
            if (!file.exists()) {
                try {
                    FileOutputStream oFile = new FileOutputStream(path, true);
                    int i = 0;
                    int len = old.size();
                    for (; i < len; i++) {
                        properties.setProperty(String.valueOf(i + 1), old.get(i));
                    }
                    properties.store(oFile, "");
                    oFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
            try {
                InputStream in = new BufferedInputStream(new FileInputStream(path));
                properties.load(in);
                in.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
    
            //遍历
            Set<String> e = properties.stringPropertyNames();
            for (String one : e) {
                String key = one;
                String value = properties.getProperty(one);
                System.out.println(key + "=" + value);
            }
    
        }
    }

    输出

    1=自来水
    2=纯净水
    3=矿泉水
    4=山泉水

    保存到文件的顺序也是如此

    多少迷茫,曾经在幽幽暗暗、反反复复中追问,才知道平平淡淡从从容容才是真。再回首恍然如梦,再回首我心依旧

  • 相关阅读:
    学习计划(一)——JavaScript 殷的博客
    学习计划——巩固基础+进阶练习 殷的博客
    第二章:在HTML中使用JavaScript 殷的博客
    W3Cschool学习笔记——CSS教程 殷的博客
    W3Cschool学习笔记——HTML5基础教程 殷的博客
    PS切图篇(一)界面设置 殷的博客
    W3Cschool学习笔记——CSS3教程 殷的博客
    PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
    css怎么让图片垂直左右居中?(外层div是浮动且按照百分比排列)
    在各浏览器和各分辨率下如何让div内的table垂直水平居中?
  • 原文地址:https://www.cnblogs.com/baby123/p/11557689.html
Copyright © 2020-2023  润新知