Import a xml file from package swing

放肆的年华 提交于 2019-12-11 04:16:23

问题


I am using this code for import shutdownscheduler.xml file from my package but I am getting error:

public static Document handler() throws ParserConfigurationException, SAXException, IOException{
        String s=xmlhandler.class.getResource("shutdownscheduler.xml").toString();
        File newFile=new File(s);
        DocumentBuilderFactory documentbuilderfactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder documentbuilder=documentbuilderfactory.newDocumentBuilder();
        Document document=(Document)documentbuilder.parse(newFile);
        document.getDocumentElement();
        return document;
    }



Exception in thread "main" java.io.FileNotFoundException:
C:\Users\Rohan Kandwal\Documents\NetBeansProjects\Shutdown
Scheduler\file:\C:\Users\Rohan%20Kandwal\Documents\NetBeansProjects\Shutdown%20Scheduler\build\classes\shutdown\scheduler\shutdownscheduler.xml
(The filename, directory name, or volume label syntax is incorrect)

If I give a direct path of the file then the import is working correctly. But this will result in an error once 1 create a jar file of the project and run it on other system. So I want a way to import file from my package only.

the correct path for the file is C:\Users\Rohan Kandwal\Documents\NetBeansProjects\Shutdown Scheduler\src\shutdown\scheduler\shutdownscheduler.xml


回答1:


Try InputStream instead of File

documentbuilder.parse( xmlhandler.class.getResourceAsStream("shutdownscheduler.xml"));


来源:https://stackoverflow.com/questions/14331940/import-a-xml-file-from-package-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!