user2026615
user2026615

Reputation: 57

Deleting csv file java

I have a CSV file that I just read the values from, and have another where I put those values and also update. I will like to delete the first CSV, and rename the updated CSV. This is what I have:

try
{
    if(file.delete())
    {
        System.out.println("Successfully deleted!");
    }
    else
    {
        System.out.println("Delete Operation didn't work.");
    }

    newFile.renameTo(new File("Book1.csv"));
}

catch (IOException ioe)
{
    System.out.println("ERROR. IO Exception: " + ioe.toString());
}

I have checked for myself, and for some reason, the delete function doesn't always work.

Upvotes: 0

Views: 5632

Answers (2)

Umesh Kumar
Umesh Kumar

Reputation: 1397

i was facing similar issue , it turned out i havent closed the csv file after reading. Closed it and problem solved :-)...

Upvotes: 0

dbyrne
dbyrne

Reputation: 61011

Without more information, its impossible to say why your delete is failing. However, at the very least you should check to make sure the delete succeeds:

if(file.delete()) {
    //Handle success
} else {
    //Handle failure
}

Upvotes: 2

Related Questions