java 从文件夹读取文件_java – 从资源文件夹中读取文件

我有一个maven项目结构,我有src / main / resources / json / test.xml文件存在,我试图读取这个使用以下代码,但不能读取它.我得到找不到指定的文件.我有要将文件对象传递给unmarshal功能,我该如何使用其他apporach来执行此操作

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());

例外是

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.(FileInputStream.java:120)

at java.io.FileInputStream.(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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值