gpt4 book ai didi

java - 写入 jar 文件中的 xml 文件

转载 作者:行者123 更新时间:2023-12-02 01:59:35 24 4
gpt4 key购买 nike

我有一段代码,旨在更新包内和资源文件夹中的 xml 文件(这是一个独立的桌面项目)。当我包含 src 文件夹的路径时,此代码在 IDE 中运行良好,例如

File file = new File("src/tools/con.xml");

我的问题是,这在 IDE 之外不起作用,例如在已部署的应用程序中,因为资源被捆绑到 jar 中。我尝试过使用类加载器方法 getResource/getResourceAsStream() 但没有成功,例如

File file = new File(getClass().getResource("/tools/con.xml").toString() );

下面是我的完整代码

try{
File file = new File("src/tools/con.xml");

StreamResult result = new StreamResult(file);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
DOMSource source = new DOMSource(doc);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);

}catch (IllegalArgumentException | TransformerException e) {
throw new IOException("Failed to save the doc");
}

最佳答案

恐怕唯一的可能性是解绑该 jar,添加您的 xml 并重新捆绑它。我必须对 war 文件的 pom.xml 执行相同的操作,但我相信过程是相同的。

关于java - 写入 jar 文件中的 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51828074/

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