使用输入流读取 java 属性文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26967352/
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
Reading a java properties file using input Stream
提问by Deepak Dave
I am getting null pointer exception on using InputStream along with class Loader function but on using FileInputStream it is reading the properties file correctly.
我在使用 InputStream 和类 Loader 函数时遇到空指针异常,但在使用 FileInputStream 时,它正在正确读取属性文件。
Why I am getting this error? Given below is my code.
为什么我收到这个错误?下面给出的是我的代码。
public String readProperties()
{
String result = "";
Properties prop = new Properties();
String file = "test.properties";
//InputStream fins = getClass().getClassLoader().getResourceAsStream(file);
try
{
prop.load(new FileInputStream(file));
//prop.load(fins);
}
catch (IOException e) {
e.printStackTrace();
}
String nation = prop.getProperty("Nation");
String city = prop.getProperty("City");
String state = prop.getProperty("State");
result = "I live in "+city+" in "+state+" in "+nation;
return result;
}
回答by Yog
Make sure that you kept your test.properties file in the classpath : i.e in Src folder of your application
确保您将 test.properties 文件保存在类路径中:即在您的应用程序的 Src 文件夹中
here is the Sample Code :
这是示例代码:
package com.example;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ReadProperties {
public static void main(String[] args) {
ReadProperties r = new ReadProperties();
String result = r.readProperties();
System.out.println("Result : " + result);
}
public String readProperties()
{
String result = "";
Properties prop = new Properties();
String file = "test.properties";
InputStream fins = getClass().getClassLoader().getResourceAsStream(file);
try
{
//prop.load(new FileInputStream(file));
if(fins!=null)
prop.load(fins);
}
catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
String nation = prop.getProperty("Nation");
String city = prop.getProperty("City");
String state = prop.getProperty("State");
result = "I live in "+city+" in "+state+" in "+nation;
return result;
}
}
回答by TobiasMende
The getResourceAsStream
-method will in your case search in the package of your class.
在getResourceAsStream
你的情况-方法将在包装类的搜索。
From ClassLoad.getResource
:
来自ClassLoad.getResource
:
This method will first search the parent class loader for the resource; if the parent is null the path of the class loader built-in to the virtual machine is searched. That failing, this method will invoke findResource(String) to find the resource.
该方法将首先在父类加载器中搜索资源;如果 parent 为 null,则搜索内置到虚拟机的类加载器的路径。如果失败,此方法将调用 findResource(String) 来查找资源。
These ClassLoader
-methods are for searching the probably bundled java application (e.g. as jar file) and not for searching files beside your application, which seems, what you want to do in this case. See ClassLoader JavaDoc.
这些ClassLoader
-methods 用于搜索可能捆绑的 java 应用程序(例如作为 jar 文件),而不是用于搜索应用程序旁边的文件,这似乎是您在这种情况下想要做的事情。请参阅ClassLoader JavaDoc。
If the ClassLoader
is unable to find the resource, the getResource*
-methods will return null
and hence your code will fail (NullPointerException
-> the stream is null
).
如果ClassLoader
无法找到资源,则getResource*
-methods 将返回null
,因此您的代码将失败(NullPointerException
-> 流是null
)。
Update:If the properties-file is in the root of your project, you might try it with a /
at the beginning of the path, when using the ClassLoader. See this threadfor further information.
更新:如果属性文件位于项目的根目录中/
,则在使用 ClassLoader 时,您可以尝试在路径的开头使用 a 。请参阅此线程以获取更多信息。
回答by Joop Eggen
The code could be like this:
代码可能是这样的:
String file = "/test.properties";
InputStream fins = getClass().getResourceAsStream(file);
InputStream fins = MyClass.class.getResourceAsStream(file);
The resource is sought relative to the class of getClass()
, but is made absolute with a starting /
. Using getClass()
however means that the actual class may be from another jar, and thus it might be better to use the actual class name.
资源是相对于 的类来寻找的getClass()
,但以 开始 是绝对的/
。getClass()
然而,使用意味着实际的类可能来自另一个 jar,因此最好使用实际的类名。
In contrast to using File, a resource might be taken from a jar (a zip format). As File on Windows is case-insensitive, one might have Test.properties working on Windows using File, and not as resource or other platforms (Linux, MacOSX).
与使用 File 不同,资源可能来自 jar(zip 格式)。由于 Windows 上的 File 不区分大小写,因此可以使用 File 在 Windows 上运行 Test.properties,而不是作为资源或其他平台(Linux、MacOSX)。
Open the jar (zip) and check that test.properties
is there.
打开罐子 (zip) 并检查它test.properties
是否在那里。
For completeness sake: you can also use a ClassLoader to fetch the resource. This is nice across jar. The path then however has to be absolute, not starting with /
.
为了完整起见:您还可以使用 ClassLoader 来获取资源。这在罐子上很好。然而,路径必须是绝对的,而不是以/
.