gpt4 book ai didi

java - 从资源文件夹中读取文件

转载 作者:搜寻专家 更新时间:2023-10-30 21:22:24 25 4
gpt4 key购买 nike

我有一个 Maven 项目结构,其中有 src/main/resources/json/test.xml 文件我正在尝试使用以下代码读取它但无法读取它。我可能找不到指定的文件.我必须将文件对象传递给 unmarshal 函数,我如何使用其他方法执行此操作

File file = new File("json\\test.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(ServiceApi.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
ServiceApi customer = (ServiceApi) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer.getService().size());

异常(exception)情况是

javax.xml.bind.UnmarshalException
- with linked exception:
[java.io.FileNotFoundException: C:\Users\jayesh_shah\Downloads\dbt-dataformstub\json\test.xml (The system cannot find the path specified)]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:202)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:142)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:151)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:169)
at com.ge.stub.api.jaxb.JAXBExample.main(JAXBExample.java:17)
Caused by: java.io.FileNotFoundException: C:\Users\jayesh_shah\Downloads\dbt-dataformstub\json\test.xml (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:653)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200)
... 6 more

最佳答案

resources文件夹在构建时被maven合并到生成的classes文件夹中。因此,您可以通过以下方式获取该文件的 InputStream:

InputStream is = YourClassName.class.getResourceAsStream("/json/test.xml");

JAXB 可以从 InputStream 中解码,并且使用与定义类 YourClassNameClassLoader 相同的规则定位文件. (有关详细信息,请参阅 getResourceAsStream。)

关于java - 从资源文件夹中读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9344142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com