0

I am trying to upload a file to two different directories. But somehow it copies the file to one directory , but fails to copy in second directory.

My src directory for file is something like this

C:\path\to\temp

And my destination directories are like this

C:\path\to\destination\1 & C:\path\to\destination\2

directories 1 and 2 are getting created on the fly.

This is what I am using in my code

public final static boolean move(String src, String dest, boolean createDestDir, boolean overwrite) {
   try{
       if(src == null || src.length() == 0 || dest == null || dest.length() == 0){ 
           return false;
       }
       File srcFile = new File(src);
       if(srcFile.isFile() == false){
          return false;
       }

       String destPath = path(dest);
        String destFileName;
        if (destPath.equals(dest)) {
            destFileName = srcFile.getName();
        } else {
            destFileName = name(dest);
        }
        File destDir = new File(destPath);
        if (destDir.exists() == false) {
            if (createDestDir == false) return false;
            if (destDir.mkdirs() == false) {
                return false;
            }
        }
        File destFile = new File(destPath + destFileName);
        if (destFile.exists()) {
            if (overwrite == false) return false;
            if (destFile.delete() == false) {
                return false;
            }
        }
        return srcFile.renameTo(destFile);
   }

}

During my loop srcFile.isFile() is failing second time, it works first time, but fails second time.

yogsma
  • 10,142
  • 31
  • 97
  • 154

1 Answers1

1

The mkdirs method creates any missing parent directories in the filepath. Eg. if the destination location is C:\some\path\to\file\, then if you call mkdirs, it will check that C:\some exists, then C:\some\path, and so on. If the path does not exist, it will create that folder and keep going. When it finishes, it will only return true if it had to create any folders. If the directory structure exists, it will return false.

In your code, you have this:

File destDir = new File(destPath);
if (destDir.exists() == false) {
    if (createDestDir == false) return false;
    if (destDir.mkdirs() == false) {
        return false;
    }
}
File destFile = new File(destPath + destFileName);
if (destFile.exists()) {
    if (overwrite == false) return false;
    if (destFile.delete() == false) {
       return false;
    }
}

I'm not sure why you go through such a convoluted way of creating the directory structure. Instead of the whole createDestDir variable (which I suspect is false when it should be true), you could just call the mkdirs() method. There is no hard calling that method if the directory structure is already in place.

Then in your next bit of code, you check if the file already exists on the file system. If it does exist, and you haven't set the overwrite boolean to true, then you try to delete the file. But at no point do you call createNewFile(). You never actually create the file on the file system.

Try replacing the above code with this:

new File(destPath).mkdirs();
File destFile = new File(destPath + destFileName);
if (destFile.exists()) 
    if (!overwrite) {
        return false;
    } else {
        destFile.delete();
    }
}
destFile.createNewFile();

This will create the destination directory if necessary, check for the existance of the file (and delete it if permissible and necessary), and then create the file at that location.

Also you should check to make sure that destPath ends with a file separator -- eg. "/" in Linux/Unix or "\" in Windows -- or else destPath + destFileName will give you something wonky.

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
  • But my code is failing at srcFile.isFile() , so this should be out of question. srcFile.isFile() is returning me the "false". – yogsma Aug 13 '12 at 16:38
  • @yogsma -- If `srcFile` doesn't exist, `isFile()` will return false. Try calling `srcFile.exists()`. – Roddy of the Frozen Peas Aug 13 '12 at 16:47
  • @yogsma -- `isFile()` fails the second time because you never created the file the first time. It should work the first time, because you call it on the source file. But then you switch your srcFile to the destination file *that you never create*. Since the file is never created, `isFile()` returns false. – Roddy of the Frozen Peas Aug 13 '12 at 17:16
  • File srcFile = new File(src) should create the file everytime. Shouldn't it? – yogsma Aug 13 '12 at 17:26
  • @yogsma -- No. It creates a reference to a file *at that location*, but does not create the file. To create the file you need to call `createNewFile()`. – Roddy of the Frozen Peas Aug 13 '12 at 17:27
  • I added `createNewFile()` and now `destDir.mkdirs()` is returning false. – yogsma Aug 13 '12 at 18:39
  • @yogsma -- Well yes. `mkdirs` returns false if it didn't have to make any directories. It means that the directory structure of your output directory already exists. IE: if you're trying to create a file at C:\temp\myfile.txt and C:\temp already exists, `mkdirs` returns false. – Roddy of the Frozen Peas Aug 13 '12 at 18:46
  • I think it has something to do with threads , because I tried to debug and took sometime on second execution of loop, it showed it copied the file and also created the file, but it was a 0 byte file. – yogsma Aug 13 '12 at 19:18
  • [See this SO question](http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java). – Roddy of the Frozen Peas Aug 13 '12 at 19:54