2012-02-18 4 views
11

У меня есть структура проекта maven, в которой присутствует файл src/main/resources/json/test.xml. Я пытаюсь прочитать это, используя следующий код, но не могу его прочитать. получение не может найти файл specified.I должно передать файл объект функции распаковать, как я могу это сделать с помощью другого 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.<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 
+0

Вы можете отправить сообщение об исключении? – Rachel

+0

@Rachel - я добавил исключение –

ответ

18

папки ресурсов сливается с созданных классами maven при создании. Таким образом, вы можете получить InputStream для этого файла через:

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

JAXB может распаковать из InputStream с, а файл расположен, используя те же правила, которые используются в ClassLoader, который определен класс YourClassName. (См. getResourceAsStream для получения дополнительной информации.)

Смежные вопросы