properties文件读取

时间:2025-02-26 12:35:02
package properties;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties; public class PropertiesUtils {
private static String configName;
private static Properties properties;
private static InputStream in; public PropertiesUtils(String config) throws IOException {
configName = config;
} private void init() throws IOException {
try {
properties = new Properties();
in = new FileInputStream(configName);
properties.load(new InputStreamReader(in, "UTF-8"));
} catch (FileNotFoundException e) {
System.out.println("初始化properties文件读取对象失败");
}
} public static void main(String[] args) throws IOException {
PropertiesUtils p = new PropertiesUtils("resources/user.properties");
p.init();
System.out.println(properties.getProperty("username"));
}
}