Bishan
Bishan

Reputation: 15740

Delete files using java

In my java application, I'm using FilenameFilter to get zip files in given directory. My directory structure is looks like below.

D\:\MyFiles\data\dir1
D\:\MyFiles\data\dir2
D\:\MyFiles\data\dir3
D\:\MyFiles\data\dir4

zip files are in dir folders. I'm giving only D\\:\\MyFiles\\data to my program and it find folders start with dir using FilenameFilter and then find files ends with zip on dir folders.

Inside a for loop I'm creating new File objects for each zip files and call delete() to delete them, but they aren't deleted.

I have printed file path using getPath() method; output is looks like below.

D\:\MyFiles\data\dir1\a.zip
D\:\MyFiles\data\dir1\b.zip
D\:\MyFiles\data\dir2\b1.zip
D\:\MyFiles\data\dir3\d.zip

Then I manually created a File object as File f = new File("D/:/MyFiles/data/dir1/a.zip") and try to delete. It succeeded.

How can I delete files? How can I give the correct path?

UPDATES

This is the code what I'm using:

// this contains folders start with 'dir' in 'D:\MyFiles\data\'
    Vector<String> dirList = utl.identifyDir(conf);

File dir;
for (int i = 0; i < dirList.size(); i++) {

// in my properties file ITEM_FOLDER is written as ITEM_FOLDER=D\:\\MyFiles\\data
//  LOG.fine(conf.readConfig(Configuration.ITEM_FOLDER)); returns D:\MyFiles\data

    dir = new File(conf.readConfig(Configuration.ITEM_FOLDER)
            + File.separator + dirList.get(i));

    // this contains all the files ends with 'zip' in 'dir' folders in 'D:\MyFiles\data\'
    Vector<String> zipFiles = utl.identifyZipFiles(dir);

    for (int x = 0; x < zipFiles.size(); x++) {

        /* delete */

        File sourcePath = new File(
                conf.readConfig(Configuration.ITEM_FOLDER)
                        + File.separator + dirList.get(i)
                        + File.separator + zipFiles.get(x));

            boolean sp = sourcePath.delete();

            LOG.fine("sourcePath : " + sourcePath.getPath() + " : "
                    + sp);

                // one of LOG prints is D:\MyFiles\data\dir3\d.zip : false



    }
}           

Upvotes: 0

Views: 1160

Answers (2)

Joetjah
Joetjah

Reputation: 6132

After reading your update, I think there are 2 possible things going on here.

  1. You've still got something open in your application. You don't happen to use a FileInputStream or anything?

  2. Another process is keeping the .zip busy. Did you open that file? Try closing the explorer window or something like that.

EDIT: A checklist from an other user:

  • Check that you've got the path correct, e.g. what does file.exists() return?
  • Check that you've got permission to delete the file as the user running your application
  • Check that you haven't got an open handle to the file within your code (e.g. have you just read from it and not closed the input stream?)
  • Check that you don't have the file opened in a desktop app

Upvotes: 1

Joetjah
Joetjah

Reputation: 6132

When you create a new File-object to test, something is different then when you use getPath. Notice how all the slashes in the pathname are \ instead of /.

Upvotes: 0

Related Questions