从Java/Linux读取外部属性文件
我的任务是编写一些代码,根据我所处的环境(生产环境或测试环境)提取属性文件。因此,jar文件在每个环境中都是相同的,但是一些变量会根据执行jar的环境而变化。导演将是这样的:从Java/Linux读取外部属性文件,java,linux,jar,properties,Java,Linux,Jar,Properties,我的任务是编写一些代码,根据我所处的环境(生产环境或测试环境)提取属性文件。因此,jar文件在每个环境中都是相同的,但是一些变量会根据执行jar的环境而变化。导演将是这样的: try(InputStream theResourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("mypropertiesfile.properties")) [ProgramOne.jar Program
try(InputStream theResourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("mypropertiesfile.properties"))
[ProgramOne.jar ProgramTwo.jar ProgramThree.jar mypropertiesfile.properties]
我需要每个jar在属性文件中读取。我在读这样的东西:
try(InputStream theResourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("mypropertiesfile.properties"))
我还没有访问这些环境的权限,为了实现这段代码,我将有一个非常小的窗口。我的问题是,鉴于我的情况,这是读取属性文件的正确方法吗?这会奏效吗?我已尝试在本地计算机上加载该文件,但所有内容都返回空值。如果该文件在您的计算机上不起作用,则可能在其他计算机上不起作用 当我认为您要从同一目录中的外部文件加载属性时,您似乎正在尝试从Jar加载资源 试试这个:
与Jar位于同一目录中的
test.properties
,具有属性my property=这是一个测试
public class Main {
public static void main(String[] args){ new Main().test(); }
/** Usually gets the `JAR` directory or the `bin` when running in IDE */
public String getCodeSourcePath() throws SecurityException, URISyntaxException {
return getClass().getProtectionDomain().getCodeSource().getLocation()
.toURI().getPath();
}
public void test() {
// TODO handle exceptions, unlikely to occur unless you do something weird
String jarPath = getCodeSourcePath();
Properties properties = new Properties();
File file = new File(jarPath, "test.properties");
try (FileInputStream fis = new FileInputStream(file);) {
properties.load(fis);
}
System.out.println(properties.getProperty("my-property"));
}
}
输出:
这是一个测试
如果它在您的机器上不工作,那么它可能在另一台机器上不工作
public class Main {
public static void main(String[] args){ new Main().test(); }
/** Usually gets the `JAR` directory or the `bin` when running in IDE */
public String getCodeSourcePath() throws SecurityException, URISyntaxException {
return getClass().getProtectionDomain().getCodeSource().getLocation()
.toURI().getPath();
}
public void test() {
// TODO handle exceptions, unlikely to occur unless you do something weird
String jarPath = getCodeSourcePath();
Properties properties = new Properties();
File file = new File(jarPath, "test.properties");
try (FileInputStream fis = new FileInputStream(file);) {
properties.load(fis);
}
System.out.println(properties.getProperty("my-property"));
}
}
当我认为您要从同一目录中的外部文件加载属性时,您似乎正在尝试从Jar加载资源
试试这个:与Jar位于同一目录中的
test.properties
,具有属性my property=这是一个测试
public class Main {
public static void main(String[] args){ new Main().test(); }
/** Usually gets the `JAR` directory or the `bin` when running in IDE */
public String getCodeSourcePath() throws SecurityException, URISyntaxException {
return getClass().getProtectionDomain().getCodeSource().getLocation()
.toURI().getPath();
}
public void test() {
// TODO handle exceptions, unlikely to occur unless you do something weird
String jarPath = getCodeSourcePath();
Properties properties = new Properties();
File file = new File(jarPath, "test.properties");
try (FileInputStream fis = new FileInputStream(file);) {
properties.load(fis);
}
System.out.println(properties.getProperty("my-property"));
}
}
输出:
这是一个测试
@MarcusHolden Noproblem@MarcusHolden没问题
public class Main {
public static void main(String[] args){ new Main().test(); }
/** Usually gets the `JAR` directory or the `bin` when running in IDE */
public String getCodeSourcePath() throws SecurityException, URISyntaxException {
return getClass().getProtectionDomain().getCodeSource().getLocation()
.toURI().getPath();
}
public void test() {
// TODO handle exceptions, unlikely to occur unless you do something weird
String jarPath = getCodeSourcePath();
Properties properties = new Properties();
File file = new File(jarPath, "test.properties");
try (FileInputStream fis = new FileInputStream(file);) {
properties.load(fis);
}
System.out.println(properties.getProperty("my-property"));
}
}