gpt4 book ai didi

java - 多次读取属性文件是否占用大量内存?

转载 作者:行者123 更新时间:2023-11-29 08:15:31 24 4
gpt4 key购买 nike

我有一个读取属性文件的类。请看下面。应用程序运行时多次调用readProperties()方法,是不是这里内存有问题?


public class PropertyReader {
private static Properties configKeyValuePairs = null;
private static String configPropertiesFileName = "Config.properties";

static void readProperties() throws FileNotFoundException, IOException {
configKeyValuePairs = new Properties();
InputStream input = ConfigReader.class
.getResourceAsStream(configPropertiesFileName);

configKeyValuePairs.load(input);

input.close();
}

static String getUserName(){
//return user name which is from the properties file.
}
}




最佳答案

假设您的属性文件永远不会更改,您可以执行以下操作:

public class MyApplicationConfiguration {
private static Properties configKeyValuePairs = new Properties();
private static String configPropertiesFileName = "Config.properties";

static {
InputStream input = null;
try {
input = MyApplicationConfiguration.class
.getResourceAsStream(configPropertiesFileName);

configKeyValuePairs.load(input);

} catch (IOException e) {
// Deal with not being able to load config, could be a fatal error!
} finally {
if (input != null) {
input.close();
}
}
}

public static String getUsername() {
// ...
}

// Implement getters for other configuration key-value pairs
// DO NOT let configKeyValuePairs be returned to anyone
}

关于java - 多次读取属性文件是否占用大量内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5148739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com