使用输入流读取java属性文件
我在使用InputStream和类加载器函数时遇到空指针异常,但在使用FileInputStream时,它正在正确读取属性文件 为什么我会犯这个错误?下面是我的代码使用输入流读取java属性文件,java,Java,我在使用InputStream和类加载器函数时遇到空指针异常,但在使用FileInputStream时,它正在正确读取属性文件 为什么我会犯这个错误?下面是我的代码 public String readProperties() { String result = ""; Properties prop = new Properties(); String file = "test.properties"; //InputStream fins = getClas
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;
}
getResourceAsStream方法将在您的案例中在类的包中进行搜索
从ClassLoad.getResource:
此方法将首先在父类装入器中搜索资源;如果父级为null,则搜索内置到虚拟机的类加载器的路径。如果失败,此方法将调用findResourceString来查找资源
这些类加载器方法用于搜索可能捆绑的java应用程序,例如jar文件,而不是搜索应用程序旁边的文件,这似乎是您在本例中要做的事情。看
如果类加载器找不到资源,getResource*-方法将返回null,因此代码将失败NullPointerException->流为null
更新:如果属性文件位于项目的根目录中,则在使用类加载器时,可以在路径的开头使用/来尝试它。有关更多信息,请参阅。getResourceAsStream方法将在您的案例中在类的包中进行搜索
从ClassLoad.getResource:
此方法将首先在父类装入器中搜索资源;如果父级为null,则搜索内置到虚拟机的类加载器的路径。如果失败,此方法将调用findResourceString来查找资源
这些类加载器方法用于搜索可能捆绑的java应用程序,例如jar文件,而不是搜索应用程序旁边的文件,这似乎是您在本例中要做的事情。看
如果类加载器找不到资源,getResource*-方法将返回null,因此代码将失败NullPointerException->流为null
更新:如果属性文件位于项目的根目录中,则在使用类加载器时,可以在路径的开头使用/来尝试它。有关更多信息,请参阅。代码可能如下所示:
String file = "/test.properties";
InputStream fins = getClass().getResourceAsStream(file);
InputStream fins = MyClass.class.getResourceAsStream(file);
相对于getClass的类查找资源,但使用起始/使其成为绝对资源。然而,使用getClass意味着实际的类可能来自另一个jar,因此最好使用实际的类名
与使用文件不同,资源可能来自jar或zip格式。由于Windows上的文件不区分大小写,因此可能有Test.properties使用文件在Windows上工作,而不是作为资源或其他平台Linux、MacOSX
打开jar zip并检查test.properties是否存在
为了完整性:您还可以使用类加载器来获取资源。这个罐子很漂亮。但是,路径必须是绝对路径,而不是以/开头。代码可能是这样的:
String file = "/test.properties";
InputStream fins = getClass().getResourceAsStream(file);
InputStream fins = MyClass.class.getResourceAsStream(file);
相对于getClass的类查找资源,但使用起始/使其成为绝对资源。然而,使用getClass意味着实际的类可能来自另一个jar,因此最好使用实际的类名
与使用文件不同,资源可能来自jar或zip格式。由于Windows上的文件不区分大小写,因此可能有Test.properties使用文件在Windows上工作,而不是作为资源或其他平台Linux、MacOSX
打开jar zip并检查test.properties是否存在
为了完整性:您还可以使用类加载器来获取资源。这个罐子很漂亮。但是,路径必须是绝对路径,而不是以/开头。请确保将test.properties文件保存在应用程序的classpath:即Src文件夹中 以下是示例代码:
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;
}
}
确保将test.properties文件保存在应用程序的classpath:即Src文件夹中 以下是示例代码:
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;
}
}
那么您显示的代码没有给出错误?这是一种令人困惑的提问方式。如果问题是当您将其作为资源加载时,我们需要更多地了解您希望资源在执行时如何出现——这是从jar文件加载,还是仅从类加载?您的资源是否被复制到类路径中?您的代码是否在包中?我的test.properties文件与src和bin文件夹一起位于我的项目的根目录中。它正在从类中加载。我的代码路径是TestNG_-trial->src->AdactIn_-SH->PropertiesDemo.java。我尝试了一些其他的建议,但还是出现了NullPointerException。我还尝试在path中的环境变量中设置属性文件的路径,以及在系统变量中设置类路径。无法理解为什么它无法读取属性文件。所以您显示的代码没有给出错误?这是一种令人困惑的提问方式。如果问题是当您将其作为r加载时
esource,我们需要更多地了解您期望资源在执行时是如何出现的——这是从jar文件加载,还是仅仅从类加载?您的资源是否被复制到类路径中?您的代码是否在包中?我的test.properties文件与src和bin文件夹一起位于我的项目的根目录中。它正在从类中加载。我的代码路径是TestNG_-trial->src->AdactIn_-SH->PropertiesDemo.java。我尝试了一些其他的建议,但还是出现了NullPointerException。我还尝试在path中的环境变量中设置属性文件的路径,以及在系统变量中设置类路径。无法理解为什么无法读取属性文件。