Java 如何从属性文件中的资源文件夹中读取文件
我在resources文件夹下有一个文件Java 如何从属性文件中的资源文件夹中读取文件,java,spring-mvc,properties-file,Java,Spring Mvc,Properties File,我在resources文件夹下有一个文件src/test/resources/file.xml 另一个在src/test/resources/test.properties下。我想在属性文件中将属性设置为指向file.xml。我怎样才能做到这一点 说我有财产 test.file = file.xml 我的java类读取文件如下: File cert = new File(fileName); // fileName is the value of test.file 但是,这不起作用。您可以
src/test/resources/file.xml
另一个在src/test/resources/test.properties下。我想在属性文件中将属性设置为指向file.xml
。我怎样才能做到这一点
说我有财产
test.file = file.xml
我的java类读取文件如下:
File cert = new File(fileName); // fileName is the value of test.file
但是,这不起作用。您可以使用类读取和写入配置文件。您可以使用类读取和写入配置文件。
- 首先,您需要找到资源使用的相对路径
以下步骤
- 其次,您可以配置和加载测试属性文件
- 最后,读取属性值并附加资源
目录
代码:
String rootDirectory=System.getProperty("user.dir");
String resourceDirectory=rootDirectory+"src/test/resources/";
//Configure property File
Properties properties = new Properties();
properties.load(new FileInputStream(resourceDirectory+"test.properties"));
PropertyConfigurator.configure(properties);
//To get the property value
String tempFileName=properties.getProperty("test.file");
//filename Needs to be changed as below
File cert = new File(resourceDirectory+tempFileName);
- 首先,您需要找到资源使用的相对路径
以下步骤
- 其次,您可以配置和加载测试属性文件
- 最后,读取属性值并附加资源
目录
代码:
String rootDirectory=System.getProperty("user.dir");
String resourceDirectory=rootDirectory+"src/test/resources/";
//Configure property File
Properties properties = new Properties();
properties.load(new FileInputStream(resourceDirectory+"test.properties"));
PropertyConfigurator.configure(properties);
//To get the property value
String tempFileName=properties.getProperty("test.file");
//filename Needs to be changed as below
File cert = new File(resourceDirectory+tempFileName);
问题是什么还不太清楚。您得到的是什么错误?可能的副本您需要完整路径或URI
实例来创建的实例。@question\u您需要使用相对路径来获得完整名称。请核对我的回答。问题是什么还不太清楚。您得到的是什么错误?可能的副本您需要完整路径或URI
实例来创建的实例。@question\u您需要使用相对路径来获得完整名称。请核对我的答案