Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8385579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:42:23+00:00 2026-06-09T17:42:23+00:00

I am trying to upload a file to two different directories. But somehow it

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:42:24+00:00Added an answer on June 9, 2026 at 5:42 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to upload file(s) using PrototypeJs request method but I failed. The
I'm trying to upload a file .txt in my web space, but then the
I am trying to upload two files at once (two file fields) with codeigniters
I have an upload form with two file upload fields. I am trying to
I'm having two problems when trying to configure the Struts 2 File Upload Interceptor
I'm trying to upload two files with one form post however my form will
I am trying to combined these two script (a file upload) and (a mysql
I am trying to upload file to an sharepoint online server using webclient. Up
I am trying to upload a file with playframework. I create a form and
I'm trying to upload a file to an FTP server using code based on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.