Soni Sahai
Soni Sahai

Reputation: 13

previous file deletion process

I am stuck up in a odd situation that is I am creating a file in a folder but I need to make sure that before the creation of a file if any file is there in the folder then it must be deleted only the current file which is process should be there.

since in my application every day a job runs which create the file in that folder so when ever presently job is running it should delete previous day file and no file should be there in afolder but the code that is shown below creates the file in that folder but the issue is that previous day file or if the job run multiple time on the same day also then those files are also thhere in the folder which should be deleted please advise how to achieve this..

File file = new File(FilePath + s); //path is c:\\abc folder & s is file name fgty.dat file         
if (file.exists()) {
  file.delete(); 
}
file.createNewFile();

Please advise

Upvotes: 0

Views: 124

Answers (4)

Henry Keiter
Henry Keiter

Reputation: 17168

Without seeing more of your code, it sounds like you just need to empty the folder before opening a new file, since right now you're only deleting the file with the exact name that you're going to write. Use the list method of file objects.

File newFile = new File(FilePath + s);
for (File f : new File(FilePath).listFiles()) { // For each file in the directory, delete it.
    f.delete();
}
newFile.createNewFile();

Note that this won't work if your folder contains other non-empty directories; you'll need a more robust solution. But the code above will at least delete all the files in the folder (barring Exceptions obviously) before creating the new file.

If, as you mentioned in the comments, you only want to delete *.dat files, it's as simple as putting a check in before you delete anything.

for (File f : new File(FilePath).listFiles()) { // For each file in the directory, delete it.
    if (f.getName().endsWith(".dat")) { // Only delete .dat files
        f.delete();
    }
}

Upvotes: 1

C.Champagne
C.Champagne

Reputation: 5489

First I think you can have problems with the way you instanciate your Fileobject because if you don't have your path separator (\), you will try to create c:\abcfgty.dat instead of c:\abc\fgty.dat.

Use instead :

File file = new File(filePath, s);

Then you can delete the files ending by ".dat". As I understood, you don't need to delete sub directories. (Here is a link that tells you how. See also here)

for (File f : filePath.list()) { // For each file in the directory, delete it.
    if(f.isFile() && file.getName().toLowerCase().endsWith(".dat");){
        f.delete();
    }
}
try {
    file.createNewFile();
} catch (IOException ex) {
    //Please do something here, at leat ex.printStackTrace()
}

Note that we can use a FileFilter to select the files to delete.

EDIT

As it was suggested in other answers, it might be preferable to move or rename the existing files instead of deleting them directly.

Upvotes: 0

Ragavan
Ragavan

Reputation: 997

File file = new File(FilePath+"test.txt");
File folder = new File(FilePath);
File[] listOfFiles = folder.listFiles();

for(int i = 0; i < listOfFiles.length; i++) {
    if (listOfFiles[i].isFile()) {
        System.out.println("File " + listOfFiles[i].getName());
        listOfFiles[i].delete();
    }
}
    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

Upvotes: 0

Nicola Musatti
Nicola Musatti

Reputation: 18218

In your place I'd move the directory to a different name, say abc.OLD, recreate it and then create your file. If everything goes well, at the end you can remove the ols directory.

If different instances of your program could be running at the same time you need to implement some form of synchronization. A rather simplistic approach could be to check if the abc.OLD directory exists and abort execution if it does.

Upvotes: 1

Related Questions