如何在读取属性文件时保留有序插入?
这个问题是自我解释的。阅读属性文件很好。但是此示例编码保留属性文件中所有属性的读取和插入顺序的方式。辅助类只是扩展属性,并在内部使用Linked Hash Map来保留插入顺序。
答案 0 :(得分:0)
让我们编写一个自定义类而不是使用java.util.Properties,我们的代码将管理所有内容:读取属性文件,拆分键值对,维护文件中出现的属性顺序等这是一个示例代码:
package com.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
public class CustomProperty {
public static void main(String[] args) {
Map<String, String> properties = parse("test.properties");
System.out.println(properties);
}
public static Map<String, String> parse(String filePath) {
Map<String, String> properties = new LinkedHashMap<>();
Scanner sc;
try {
sc = new Scanner(new File(filePath));
while (sc.hasNextLine()) {
try {
String[] tokens = sc.nextLine().split("=");
properties.put(tokens[0].trim(), tokens[1].trim());
} catch (Exception e) {
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return properties;
}
}
答案 1 :(得分:-1)
**application.properties**
banner.charset=UTF-8
banner.location=classpath:banner.txt
server.port=8080
server.contextPath=/test
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
server.session.timeout=18000
server.session.cookie.max-age=18000
management.security.enabled=false
security.basic.enabled=false
a=1
b=2
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
// Load and Read a property file from class path
public class Load_and_Print_a_Property_File_From_Classpath2 {
public static void main(String[] args) {
PreserveInsertionOrderedProperties prop = new PreserveInsertionOrderedProperties();
InputStream input = null;
String filename = "application.properties";
try {
input = Load_and_Print_a_Property_File_From_Classpath2.class.getClassLoader().getResourceAsStream(filename);
if (input == null) {
System.out.println("Sorry, unable to find " + filename);
return;
}
prop.load(input);
LinkedHashMap<Object, Object> keyValueSet = prop.keyValueSet;
for (Object key : keyValueSet.keySet()) {
System.out.println("Key = " + key + " | " + "Value = " + keyValueSet.get(key));
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class PreserveInsertionOrderedProperties extends Properties {
private static final long serialVersionUID = 1L;
public LinkedHashMap<Object, Object> keyValueSet = new LinkedHashMap<Object, Object>();
@Override
public Enumeration<Object> keys() {
return Collections.enumeration(keyValueSet.keySet());
}
@Override
public Set<Object> keySet() {
return keyValueSet.keySet();
}
@Override
public synchronized Object put(Object key, Object value) {
if (!keyValueSet.containsValue(key))
keyValueSet.put(key.toString(), value.toString());
return keyValueSet.get(key);
}
@Override
public synchronized Object remove(Object key) {
return keyValueSet.remove(key);
}
@Override
public synchronized void putAll(Map values) {
for (Object key : values.keySet()) {
if (!containsKey(key)) {
keyValueSet.putAll(values);
}
}
}
}
**After print**
Key = banner.charset | Value = UTF-8
Key = banner.location | Value = classpath:banner.txt
Key = server.port | Value = 8080
Key = server.contextPath | Value = /test
Key = spring.mvc.view.prefix | Value = /WEB-INF/jsp/
Key = spring.mvc.view.suffix | Value = .jsp
Key = server.session.timeout | Value = 18000
Key = server.session.cookie.max-age | Value = 18000
Key = management.security.enabled | Value = false
Key = security.basic.enabled | Value = false
Key = a | Value = 1
Key = b | Value = 2