Java 用流结果覆盖文件

发布于 2024-10-16 14:44:57 字数 1846 浏览 2 评论 0原文

我创建了一个简单的类来创建 XML 文档。 但是,如果我在创建同名文档时多次调用该方法,则该文件不会被覆盖。 如何使该类自动覆盖同名的现有文件?

import java.io.File;

导入 javax.xml.parsers.DocumentBuilder; 导入 javax.xml.parsers.DocumentBuilderFactory; 导入 javax.xml.parsers.ParserConfigurationException; 导入 javax.xml.transform.Transformer; 导入 javax.xml.transform.TransformerConfigurationException; 导入 javax.xml.transform.TransformerException; 导入 javax.xml.transform.TransformerFactory; 导入 javax.xml.transform.dom.DOMSource; 导入 javax.xml.transform.stream.StreamResult;

导入 org.w3c.dom.Document; 导入 org.w3c.dom.Element;

公共类 XMLCreater { 公共静态无效CreateXMLDoc(字符串名称,字符串根,字符串[]元素,字符串[]子元素)抛出TransformerConfigurationException { 尝试 { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); 文档 doc = docBuilder.newDocument(); 元素 rootElement = doc.createElement(root); doc.appendChild(rootElement);

for (int i = 0; i < elements.length; i ++) { Element element = doc.createElement(elements[i]); element.appendChild(doc.createTextNode(children[i])); rootElement.appendChild(element); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); File dir = new File(System.getProperty("user.dir")); StreamResult result = new StreamResult(new File(dir + "\\XML\\" + name + ".xml")); transformer.transform(source, result); } catch(ParserConfigurationException pce){ pce.printStackTrace(); } catch(TransformerException tfe) { tfe.printStackTrace(); } }

}

I created a simple class to create an XML document.
However, if I call the method more than once while creating a document of the same name the file does not overwrite.
How could I make the class automatically overwrite existing files of the same name?

import java.io.File;

import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document; import org.w3c.dom.Element;

public class XMLCreater { public static void CreateXMLDoc(String name, String root, String[] elements, String[] children) throws TransformerConfigurationException { try { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement(root); doc.appendChild(rootElement);

for (int i = 0; i < elements.length; i ++) { Element element = doc.createElement(elements[i]); element.appendChild(doc.createTextNode(children[i])); rootElement.appendChild(element); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); File dir = new File(System.getProperty("user.dir")); StreamResult result = new StreamResult(new File(dir + "\\XML\\" + name + ".xml")); transformer.transform(source, result); } catch(ParserConfigurationException pce){ pce.printStackTrace(); } catch(TransformerException tfe) { tfe.printStackTrace(); } }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£冰雨忧蓝° 2024-10-23 14:44:58

我用以下语句执行了你的代码:

public static void main (String[] args) 
{
    XMLCreater x = new XMLCreater();
    String[] s = {"A","B","C"};
    try 
    {
        x.CreateXMLDoc("k","root",s,s);
        x.CreateXMLDoc("k","root",s,s);
        x.CreateXMLDoc("fakih","root",s,s);
    }
    catch (TransformerConfigurationException exception) 
    { exception.printStackTrace(); }
}

并且它很好地覆盖了现有文件,没有覆盖问题,请自行检查。

I executed your code with the following statements:

public static void main (String[] args) 
{
    XMLCreater x = new XMLCreater();
    String[] s = {"A","B","C"};
    try 
    {
        x.CreateXMLDoc("k","root",s,s);
        x.CreateXMLDoc("k","root",s,s);
        x.CreateXMLDoc("fakih","root",s,s);
    }
    catch (TransformerConfigurationException exception) 
    { exception.printStackTrace(); }
}

And it nicely overwrites the existing files, no problems about overwriting, check it yourself.

浮华 2024-10-23 14:44:58

我在这里说实话......我无法复制你的问题。当我在 for 循环中多次运行该程序时,它对我来说效果很好。您确定在运行程序之前没有意外打开结果文件从而锁定它吗?

如果您担心多个线程同时运行您的程序,也许您可​​以应用同步块来防止两个线程尝试写入同一文件,如下所示:-

...

synchronized (XMLCreater.class) {
    StreamResult result = new StreamResult(new File(dir + "\\XML\\" + name + ".xml"));
    transformer.transform(source, result);
}

I'll be honest here... I'm not able to replicate your problem. It works fine for me when I run this program multiple times in a for loop. Are you sure you didn't accidentally open the result file, thus locking it, before running your program?

If you are concerned of having multiple threads running your program at the same time, perhaps you can apply a synchronized block to prevent two threads trying to write the same file, like this:-

...

synchronized (XMLCreater.class) {
    StreamResult result = new StreamResult(new File(dir + "\\XML\\" + name + ".xml"));
    transformer.transform(source, result);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文