Java 用流结果覆盖文件
我创建了一个简单的类来创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我用以下语句执行了你的代码:
并且它很好地覆盖了现有文件,没有覆盖问题,请自行检查。
I executed your code with the following statements:
And it nicely overwrites the existing files, no problems about overwriting, check it yourself.
我在这里说实话......我无法复制你的问题。当我在 for 循环中多次运行该程序时,它对我来说效果很好。您确定在运行程序之前没有意外打开结果文件从而锁定它吗?
如果您担心多个线程同时运行您的程序,也许您可以应用同步块来防止两个线程尝试写入同一文件,如下所示:-
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:-