gpt4 book ai didi

java - 如何将属性文件加载到Java中?

转载 作者:行者123 更新时间:2023-12-02 10:04:19 24 4
gpt4 key购买 nike

我正在尝试将属性文件加载到 linux 目录上的 Java 中。连接属性:

hiveDriver=HiveDriver
hiveServer=ip-1-2-1-1.
hivePort=123
hiveUser=huser
hivePassword=etl123
gpDriver=org.postgresql.Driver
metaStoreUrl=metaurl
port=5432
metaUser=devusr
metaPassword=abcdefg
gpAnalyticsServer=1.2.3.4.5
gpUser=gpuser
gpPassword=09987665

代码:

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

try {
Properties props = new Properties();
String propFile = "/home/devuser/connection.properties";
InputStream inputStream = StartCount.class.getClassLoader().getResourceAsStream(propFile);
if(inputStream != null) {
props.load(inputStream);
}
String hiveDriver = props.getProperty("hiveDriver");
String hiveServer = props.getProperty("hiveServer");
String hivePort = props.getProperty("hivePort");
String hiveUser = props.getProperty("hiveUser");
String hivePassword = props.getProperty("hivePassword");
String gpDriver = props.getProperty("gpDriver");
String hiveMetaStoreServer = props.getProperty("hiveMetaStoreServer");
String port = props.getProperty("port");
String hiveMetaUser = props.getProperty("hiveMetaUser");
String hiveMetaPassword = props.getProperty("hiveMetaPassword");
String gpAnalyticsServer = props.getProperty("gpAnalyticsServer");
String gpUser = props.getProperty("gpUser");
String gpPassword = props.getProperty("gpPassword");
System.out.println(hiveDriver) ;
System.out.println(hiveServer);
System.out.println(hivePort);
System.out.println(hiveUser);
System.out.println(hivePassword);
System.out.println(gpDriver);
System.out.println(hiveMetaStoreServer);
System.out.println(port);
System.out.println(hiveMetaUser);
System.out.println(hiveMetaPassword);
System.out.println(gpAnalyticsServer);
System.out.println(gpUser);
System.out.println(gpPassword);
} catch(Exception e) {
e.printStackTrace();
}

我从保存“connection.properties”的同一位置提交 jar。当我运行代码时,我看到 println 语句打印 null 。谁能让我知道我在上面的代码中犯了什么错误?

最佳答案

InputStream inputStream = new FileInputStream(propFile);

就是这样...

因为

StartCount.class.getClassLoader().getResourceAsStream(propFile) != propFile

关于java - 如何将属性文件加载到Java中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55416441/

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