使用java.util.Properties提供的類,讀取properties文件的時候,讀出來的是亂序的java
以下邊的狀況this
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); } } }
輸出spa
4=山泉水
3=礦泉水 2=純淨水 1=自來水
保存到文件的順序也是如此code
那若是想是有序的,怎麼辦呢?blog
自定義一個Properties 類get
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; } }
使用string
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); } } }
輸出it
1=自來水
2=純淨水 3=礦泉水 4=山泉水
保存到文件的順序也是如此io
多少迷茫,曾經在幽幽暗暗、反反覆覆中追問,才知道平平淡淡從從容容纔是真。再回首恍然如夢,再回首我心依舊class