Java 如何在读取属性文件时保持插入顺序?
如何在读取属性文件时保持插入顺序 这个问题是自我解释的。读取属性文件是可以的。但本例编码了保留属性文件中所有属性的读取和插入顺序的方法。helper类只是扩展属性,并在内部使用链接的哈希映射来保留插入顺序。Java 如何在读取属性文件时保持插入顺序?,java,Java,如何在读取属性文件时保持插入顺序 这个问题是自我解释的。读取属性文件是可以的。但本例编码了保留属性文件中所有属性的读取和插入顺序的方法。helper类只是扩展属性,并在内部使用链接的哈希映射来保留插入顺序。**application.Properties** **application.properties** banner.charset=UTF-8 banner.location=classpath:banner.txt server.port=8080
**application.Properties**
**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
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
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.Collections;
导入java.util.Enumeration;
导入java.util.LinkedHashMap;
导入java.util.Map;
导入java.util.Properties;
导入java.util.Set;
//从类路径加载并读取属性文件
公共类从类路径2加载和打印属性文件{
公共静态void main(字符串[]args){
PreserveInsertionOrderedProperties prop=新的PreserveInsertionOrderedProperties();
InputStream输入=null;
字符串filename=“application.properties”;
试一试{
输入=从\u Classpath2.class.getClassLoader().getResourceAsStream(文件名)加载\u和\u打印\u属性\u文件\u;
如果(输入==null){
System.out.println(“对不起,找不到”+文件名);
返回;
}
道具载荷(输入);
LinkedHashMap keyValueSet=prop.keyValueSet;
for(对象键:keyValueSet.keySet()){
System.out.println(“Key=“+Key+”|“+”Value=“+keyValueSet.get(Key));
}
}捕获(IOEX异常){
例如printStackTrace();
}最后{
如果(输入!=null){
试一试{
input.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
}
}
公共类PreserveInsertionOrderedProperties扩展属性{
私有静态最终长serialVersionUID=1L;
public LinkedHashMap keyValueSet=新LinkedHashMap();
@凌驾
公共枚举密钥(){
返回Collections.enumeration(keyValueSet.keySet());
}
@凌驾
公共集密钥集(){
返回keyValueSet.keySet();
}
@凌驾
公共同步对象put(对象键、对象值){
如果(!keyValueSet.containsValue(键))
keyValueSet.put(key.toString(),value.toString());
返回keyValueSet.get(键);
}
@凌驾
公共同步对象删除(对象密钥){
返回keyValueSet。删除(键);
}
@凌驾
公共同步void putAll(映射值){
for(对象键:values.keySet()){
如果(!containsKey(键)){
keyValueSet.putAll(值);
}
}
}
}
**印后**
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
键=a |值=1
键=b |值=2
让我们编写一个自定义类,而不是使用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;
}
}
package.com.test;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.LinkedHashMap;
导入java.util.Map;
导入java.util.Scanner;
公共类自定义属性{
公共静态void main(字符串[]args){
映射属性=解析(“test.properties”);
System.out.println(属性);
}
公共静态映射解析(字符串文件路径){
映射属性=新建LinkedHashMap();
扫描仪sc;
试一试{
sc=新扫描仪(新文件(文件路径));
while(sc.hasNextLine()){
试一试{
字符串[]标记=sc.nextLine().split(“”);
properties.put(令牌[0].trim(),令牌[1].trim());
}捕获(例外e){
}
}
}catch(filenotfounde异常){
e、 printStackTrace();
}
归还财产;
}
}
实现不好,根据使用情况给出不同的结果。什么示例?在这个问题上没有例子。你为什么需要保留属性的顺序?