1

我的第一个问题是关于 stax writer 的速度。现在我有一个问题:我无法删除我创建的 XML 文件,delete();

它仍在以某种方式使用。我试图手动删除它,但我不能。
在创建文件时,我调用close()StreamWriter 的方法。

我正在尝试在此方法完成后立即删除另一个文件中的文件。类似的东西: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();
}
4

2 回答 2

3

关闭 XMLStreamWriter 不会关闭基础输出流。

看:

http://docs.oracle.com/javaee/5/api/javax/xml/stream/XMLStreamWriter.html#close()

于 2013-05-04T19:16:36.217 回答
0

有趣的是,虽然它可能是违反直觉的,但如果您在 Windows 上使用 Sun JVM,那么厚颜无耻地调用 System.gc() 可能会成功。是的,是的,我知道,我们“从不”必须明确调用 gc,并且“不能保证”运行等等。但相信我,这是一个有时会出现的已知问题。

于 2013-05-07T14:00:45.670 回答