java 如何读取jar中的属性文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29070109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read properties file inside jar?
提问by NaveenBharadwaj
I have a config.properties file under the package com.abc.properties. From one of the java class present in com.abc.util, I need to read the property file. Both the files are present inside jar. I have tried using
我在包 com.abc.properties 下有一个 config.properties 文件。从 com.abc.util 中的 Java 类之一,我需要读取属性文件。这两个文件都存在于 jar 中。我试过使用
fs = new FileInputstream(VerifyFolderStructure.class.getResourceAsStream("com/abc/properties/config.properties"));
But it doesn't seem to work. Please help.
但它似乎不起作用。请帮忙。
P.S: VerifyFolderStructure is my java class from which I need to load the properties file.
PS:VerifyFolderStructure 是我的 java 类,我需要从中加载属性文件。
回答by StanislavL
To obtain the stream you don't need FileInputStream at all. You can get the properties' file stream like this
要获取流,您根本不需要 FileInputStream。您可以像这样获取属性的文件流
InputStream is = VerifyFolderStructure.class.getResourceAsStream("/com/abc/properties/config.properties");
回答by Gospel
I think you need to add "/" in the front of you path.if you don't add "/" that means the properties is located in the same packages path as VerifyFolderStructure class.
我认为您需要在路径前面添加“/”。如果不添加“/”,则表示属性与 VerifyFolderStructure 类位于同一包路径中。