簡體   English   中英

為什么我不能刪除我創建的XML文件?

[英]Why can't I delete the XML file I created?

我的第一個問題是關於stax writer的速度。 現在我遇到了一個問題:我無法使用delete();我創建的XML文件delete();

它仍在以某種方式使用。 我試圖手動刪除它,但我不能。
在文件創建時,我為StreamWriter調用方法close()

我試圖在此方法完成后,在另一個文件中刪除文件。 像: startstopwath;createxml(); stopstopwatch; file.delete() startstopwath;createxml(); stopstopwatch; file.delete() 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();
}

關閉XMLStreamWriter不會關閉基礎輸出流。

看到:

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

有趣的是,盡管它可能是反直覺的,如果你在Windows上使用Sun JVM,那么對System.gc()的厚顏無恥的調用可能會有所幫助。 是的,是的,我知道,我們“從不”必須明確地調用gc,並且“不保證”運行等等。但請相信我,這是一個有時會出現的已知問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM