我的first question是关于stax作家的速度。现在我遇到了一个问题:我无法删除我创建的XML文件,其中包含delete();
为什么我不能删除我创建的XML文件?
它仍在以某种方式使用。我试图手动删除它,但我不能。
在文件创建过程中,我为StreamWriter调用方法close()
。
我想在此方法完成后,在另一个文件中删除文件。 类似于:startstopwath;createxml(); stopstopwatch; file.delete()
我需要在此添加更多内容吗?
这里是我的代码:
import java.io.*;
import java.util.*;
import javax.xml.stream.*;
import org.springframework.util.StopWatch;
public class StAXVytvor {
public static String subor = "test.xml";
public static void main(String[] args) {
int num =600000;
try {
String encoding = "UTF-8";
XMLOutputFactory f = XMLOutputFactory.newInstance();
XMLStreamWriter w = f.createXMLStreamWriter(new BufferedOutputStream(newFileOutputStream(subor)), "UTF-8");
w.writeStartDocument(encoding, "1.0");
w.writeCharacters("\r\n");
w.writeStartElement("Noviny");
for (int i = 1; i <= num; i++) {
w.writeCharacters("\r\n ");
w.writeStartElement("Author");
w.writeCharacters("\r\n ");
w.writeStartElement("Id");
String ID = Integer.toString(i);
w.writeCharacters(ID);
w.writeEndElement();
w.writeCharacters("\r\n ");
w.writeStartElement("Meno");
w.writeCharacters("Autor"+i);
w.writeEndElement();
w.writeCharacters("\r\n ");
w.writeStartElement("Email");
w.writeCharacters("Author"+i+"@email.com");
w.writeEndElement();
w.writeCharacters("\r\n ");
w.writeStartElement("phone");
w.writeAttribute("type", "pevna");
w.writeCharacters("+4219");
w.writeEndElement();
w.writeCharacters("\r\n ");
w.writeStartElement("plat");
w.writeCharacters("5000");
w.writeEndElement();
w.writeCharacters("\r\n ");
w.writeEndElement();
w.writeCharacters("\r\n");
}
w.writeCharacters("\r\n");
w.writeEndElement();
w.writeCharacters("\r\n");
w.writeEndDocument();
w.close();
System.out.println("success");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
我想在这里删除:
domvytvor.main(args);
try {
File file = new File ("STAX.xml");
if(file.delete() && file1.delete()){
System.out.println(file.getName() + " is deleted!");
}
else{
System.out.println("Delete operation is failed.");
}
}
catch(Exception e)
{
e.printStackTrace();
}
您应该关闭'finally'块中的资源。在何处以及如何定义'file1'? – skirsch 2013-05-04 19:04:29