[英]get file from src/test/resources using getResourceAsStream()
++add i want to validate json based on json schema file. ++添加我想基于json模式文件验证json。 I currently save json as string , and try to read json schema file(which is test.json), then validate.
我目前将json保存为string,并尝试读取json模式文件(即test.json),然后进行验证。 however, reading schema file is always null..
但是,读取架构文件始终为null。
below code are I tried so far. 到目前为止,我尝试了以下代码。
++original post. ++原始帖子。
this is my current situaction. 这是我目前的情况。 I've tried many ways to get the file(test.json)...
我已经尝试了多种方法来获取文件(test.json)...
like 喜欢
InputStream in;
1. in= valid.class.getResourceAsStream("/test.json");
2. in = getClass().getClassLoader().getResourceAsStream("/test.json");
3. in = Thread.currentThread().getContextClassLoader().getResourceAsStream("//test.json");
4. in=getClass().getResourceAsStream("/test.json");
5. in=ValidationUtils.class.getResourceAsStream("../resources/ds/test.json");
6. ClassLoader classLoader = getClass().getClassLoader();
in = classLoader.getResourceAsStream("test.json");
7.in=getClass().getResourceAsStream("/ds/test.json"); //tried just now
System.out.println("in"+in); //this is always null.
nothing.. work.. above.. please help me... 没有..工作..以上..请帮助我...
You can't (by default) access src/test/resources
resources from code within src/main/java
. 您(默认)不能从
src/main/java
代码访问src/test/resources
资源。 All src/test/resources
resources are only available for tests usually stored in src/test/java
and executed during test
phase (in Maven). 所有
src/test/resources
资源仅适用于通常存储在src/test/java
并在test
阶段(在Maven中)执行的test
。 During that phase, test code can access that resources by some (not all) of the methods you quoted in your question. 在该阶段, 测试代码可以通过您在问题中引用的某些(不是全部)方法来访问该资源。
For your main classes, the resources root is src/main/resources
. 对于您的主类,资源根目录为
src/main/resources
。 You should store your JSON schema file there. 您应该在其中存储JSON模式文件。 Test code can also access resources stored there (so no duplication needed), but not vice versa.
测试代码也可以访问存储在其中的资源(因此不需要重复),反之亦然。
When getting resources, the resources folder is the root. 获取资源时,资源文件夹是根目录。 If the file is in a subfolder, you need to include it in your path.
如果文件在子文件夹中,则需要将其包括在路径中。 Use
采用
in = getClass().getResourceAsStream("/ds/test.json");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.