简体   繁体   English

FileInputStream找不到文件智能提示

[英]FileInputStream can not find file intelliJ idea

I'm writing a DBUnit test. 我正在编写一个DBUnit测试。 When exporting data from an xml-file, the compiler produces the following error: 从xml文件导出数据时,编译器会产生以下错误:

java.io.FileNotFoundException: ru/iteco/blockchain/rzd/cred/core/server/db/hibernate/dao/contract/contract-data.xml (No such file or directory).

Here is the corresponding part of the code: 这是代码的相应部分:

@Before
public void setUp() throws Exception {
    super.setUp();
    beforeData = new FlatXmlDataSet(
            new InputStreamReader(new FileInputStream(
                    "ru/iteco/blockchain/rzd/cred/core/server/db/hibernate/dao/contract/contract-data.xml"),
                    "utf-8"));
    tester.setDataSet(beforeData);
    tester.onSetup();
}

The variable beforeData is of type IDataSet. 变量beforeData的类型为IDataSet。 The Java class resides in the folder named Java类驻留在名为的文件夹中

/src/test/java/ru/iteco/blockchain/rzd/cred/core/server/db/hibernate/dao/ContractDAOImplTest.java

The xml-file resides in the folder named xml文件位于名为的文件夹中

/src/test/resources/ru.iteco.blockchain.rzd.cred.core.server.db.hibernate.dao.contract/contract-data.xml

I will be very grateful if anyone can explain what the cause of the error is. 如果有人能解释错误的原因,我将不胜感激。

"ru/iteco/blockchain/rzd/ “ ru / iteco / blockchain / rzd /

This is a relative path, which means it is based on current directory. 这是一个相对路径,这意味着它基于当前目录。 The error means it does not exist relative to the current directory you run it from. 该错误表示相对于您从中运行它的当前目录不存在。

Best to load resource files from the classpath to eliminate the relative directory problem, eg this.getClass().getResource(filename) . 最好从类路径加载资源文件以消除相对目录问题,例如this.getClass().getResource(filename)

Consider using FlatXmlDataFileLoader.load(String filename) as it does it correctly for you (that method is in its parent class). 考虑使用FlatXmlDataFileLoader.load(String filename)因为它可以为您正确地执行操作(该方法在其父类中)。

Update: 更新:

I did not quite understand what I should use from the proposed one. 我不太了解我应该从建议的内容中使用什么。

If "ru/iteco/blockchain/rzd/cred/core/server/db/hibernate/dao/contract/contract-data.xml" is on the classpath, then 如果“ ru / iteco / blockchain / rzd / cred / core / server / db / hibernate / dao / contract / contract-data.xml”在类路径上,则

beforeData = new FlatXmlDataFileLoader.load("/ru/iteco/blockchain/rzd/cred/core/server/db/hibernate/dao/contract/contract-data.xml");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM