title: Why can't I delete the XML file I created?
body: <p>My <a href="https://stackoverflow.com/questions/16354252/why-does-stax-create-xml-slower-than-dom">first question</a> was about the speed of stax writer. Now I have a problem: I can't delete the XML file I created, with <code>delete();</code></p>
  
  <p>It is still being used somehow. I have tried to delete it manually but I could not.<br>
  At the and of file creation I call method <code>close()</code> for StreamWriter.</p>
  
  <p>I am trying to delete file right after this method is done, in another file.
  Something like: <code>startstopwath;createxml(); stopstopwatch; file.delete()</code> Do I have to add something more there? </p>
  
  <p>Here is my code: </p>
  
  <pre><code>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 &lt;= 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();
          }
      }
  }
  </code></pre>
  
  <p>I am trying to delete it here:</p>
  
  <pre><code>domvytvor.main(args);
  try {
  
      File file = new File ("STAX.xml");
  
      if(file.delete() &amp;&amp; file1.delete() ){
          System.out.println(file.getName() + " is deleted!");
      }
      else{
          System.out.println("Delete operation is failed.");
      }
  }
  catch(Exception e)
  {
      e.printStackTrace();
  }
  </code></pre>