Nelly Junior
Nelly Junior

Reputation: 566

Delete a XML input file Java

I am reading a XML file, process elements and write another XML file with the elements extracted form the input file. For this I am using the StAX cursor: I read elements with XMLStreamReader and write elements in another file using XMLStreamWriter.

The reader:

 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new FileReader(inputFile));

The writer:

XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new FileOutputStream (outputFile),"UTF-8");

After I finish processing and writing the output file, I want to delete the file I read (the input file). For trhis I am using:

public void deleteInputFile(String inputFile) {
        File fileToDelete = new File(inputFile);
        if(fileToDelete.delete()){
            System.out.println(fileToDelete.getName() + " is deleted!");
        }else{
            System.out.println("Delete operation is failed.");
        }
    }

All program works fine, but the delete opretion fails. I get the response:

Delete operation is failed.

I think the reader close the input file after it reads it.

The question is, what showld I do to delete the input file after writing the file I need?

Upvotes: 1

Views: 2295

Answers (2)

Nelly Junior
Nelly Junior

Reputation: 566

All streams must be declared separately so you can close them separately:

// Declarring streams:
FileReader fr = new FileReader(inputFile);
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(fr);
...

// Closing streams.
fr.close();
streamReader.close();

// Deleting inputFile
deleteMethod();

Upvotes: 0

Mauricio Gracia Gutierrez
Mauricio Gracia Gutierrez

Reputation: 10862

Make sure you close the input stream and then delete the file.

You can delete files, directories or links. With symbolic links, the link is deleted and not the target of the link. With directories, the directory must be empty, or the deletion fails.

The Files class provides two deletion methods.

The delete(Path) method deletes the file or throws an exception if the deletion fails. For example, if the file does not exist a NoSuchFileException is thrown. You can catch the exception to determine why the delete failed as follows:

try {
    Files.delete(path);
} catch (NoSuchFileException x) {
    System.err.format("%s: no such" + " file or directory%n", path);
} catch (DirectoryNotEmptyException x) {
    System.err.format("%s not empty%n", path);
} catch (IOException x) {
    // File permission problems are caught here.
    System.err.println(x);
}

The deleteIfExists(Path) method also deletes the file, but if the file does not exist, no exception is thrown. Failing silently is useful when you have multiple threads deleting files and you don't want to throw an exception just because one thread did so first.

Taken from https://docs.oracle.com/javase/tutorial/essential/io/delete.html

Here is another example http://www.mkyong.com/java/how-to-delete-file-in-java/

Upvotes: 2

Related Questions