my project struture looks like
我的项目结构看起来像
project/
src/main/
java/ ...
resources/
definitions.txt
test/
CurrentTest.java
resources/ ...
In my test I need to open the definitions.txt
在我的测试中,我需要打开definition .txt
I do
我做
@Test
public void testReadDesiredDefinitions() throws PersistenceException, IOException {
final Properties definitions = new Properties();
definitions.load(new ResourceService("/").getStream("desiredDefinitions"));
System.out.println(definitions);
}
When I run this, I get
当我运行这个,我得到
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:418)
at java.util.Properties.load0(Properties.java:337)
at java.util.Properties.load(Properties.java:325)
How can I read this text file?
如何读取这个文本文件?
Thanks
谢谢
4 个解决方案
#1
12
The "current directory" of unit tests is usually the project directory, so use this:
单元测试的“当前目录”通常是项目目录,所以使用以下方法:
File file = new File("src/main/resources/definitions.txt");
and load the properties from the file:
并从文件中加载属性:
definitions.load(new FileInputStream(file));
If this doesn't work, or you want to check what the current directory is, just print out the path and it will be obvious what the current directory is:
如果这不起作用,或者您想要检查当前目录是什么,只要打印出路径,那么很明显当前目录是什么:
System.out.println(file.getAbsolutePath());
#2
1
You can make use of Class#getResourceAsStream to easily create a stream to a resource file.
您可以使用Class# getresourcestream来轻松地为资源文件创建一个流。
definitions.load(getClass().getResourceAsStream("/main/java/resources/definitions.txt"));
The location parameter should be the relative file path with regards to your project base (my guess was main).
位置参数应该是与项目基础相关的文件路径(我的猜测是主要的)。
#3
0
If your resources
directory is a source folder, you can use /resources/definitions.txt
as a correct path.
如果资源目录是源文件夹,可以使用/resources/定义。txt是正确的路径。
I don't know about ResourceService
but this should work:
我不知道ResourceService,但是这个应该可以:
final Properties definitions = new Properties();
definitions.load(getClass().getResourceAsStream("/resources/definitions.txt"))
#4
0
File file = new File("../src/main/resources/definitions.txt");
文件文件=新文件(".. ./src/main/resources/definitions.txt");
#1
12
The "current directory" of unit tests is usually the project directory, so use this:
单元测试的“当前目录”通常是项目目录,所以使用以下方法:
File file = new File("src/main/resources/definitions.txt");
and load the properties from the file:
并从文件中加载属性:
definitions.load(new FileInputStream(file));
If this doesn't work, or you want to check what the current directory is, just print out the path and it will be obvious what the current directory is:
如果这不起作用,或者您想要检查当前目录是什么,只要打印出路径,那么很明显当前目录是什么:
System.out.println(file.getAbsolutePath());
#2
1
You can make use of Class#getResourceAsStream to easily create a stream to a resource file.
您可以使用Class# getresourcestream来轻松地为资源文件创建一个流。
definitions.load(getClass().getResourceAsStream("/main/java/resources/definitions.txt"));
The location parameter should be the relative file path with regards to your project base (my guess was main).
位置参数应该是与项目基础相关的文件路径(我的猜测是主要的)。
#3
0
If your resources
directory is a source folder, you can use /resources/definitions.txt
as a correct path.
如果资源目录是源文件夹,可以使用/resources/定义。txt是正确的路径。
I don't know about ResourceService
but this should work:
我不知道ResourceService,但是这个应该可以:
final Properties definitions = new Properties();
definitions.load(getClass().getResourceAsStream("/resources/definitions.txt"))
#4
0
File file = new File("../src/main/resources/definitions.txt");
文件文件=新文件(".. ./src/main/resources/definitions.txt");