Link to home
Start Free TrialLog in
Avatar of Vakils
VakilsFlag for United States of America

asked on

Rename file in Java

Hi,
I am renaming a file in Java on Linux O/S. The function renameTo returns true but file does not get renamed half the time. The file resides on a mounted folder on Linux machine. The code is below:

        // Rename the file with an appended .LOAD extension, so that next time
    // this program will not pickup the same file again
      String renameToFile = filename + ".load";             //filename is valid file name
      File sourceFile = new File(filename);
    if (sourceFile.renameTo(new File(renameToFile)))
    {
     //success, verify now
      File destFile = new File(renameToFile);
      boolean renamed = false;
      while (TIMES_TO_RENAME_VERIFY-- > 0)             //5 times
      {
        try
        {
          Thread.sleep(RETRY_WAIT_TIME); //wait to confirm renaming        6000ms
          if (destFile.exists())
          {
            renamed = true;
            break;
          }
        }
        catch (InterruptedException ie)
        {
          log.exception("File rename wait exception ", ie);
        }
      }

      if (renamed)
        log.info("Successfully renamed the file " + filename +
                            " to append .load at the end");
      else
      {
        log.error("Failed to verify renamed file " + filename + ".load"); //this is the error I get
              return;
      }
    }
    else
    {
      log.error("Unable to rename the file " + filename + " to append .load at the end");
      return;
    }
Avatar of Michael Fowler
Michael Fowler
Flag of Australia image

Try using Files.Move instead

String renameToFile = filename + ".load";
File sourceFile = new File(filename);
try {
     Files.move(sourceFile, sourceFile.resolveSibling(renameToFile));
} catch (IOException e) {
     log.error("Unable to rename the file " + filename + " to append .load at the end");
}

Open in new window


If your code only needs to be run on Linux another option would be to use Process Builder to run a Linux command such as mv although this method would be less than ideal.

https://docs.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html
https://www.udemy.com/blog/rename-a-file-in-linux/
Avatar of Am P
Check the location where the file is being created.

       // Rename the file with an appended .LOAD extension, so that next time
    // this program will not pickup the same file again
      String renameToFile = filename + ".load";             //filename is valid file name
      File sourceFile = new File(filename);
    if (sourceFile.renameTo(new File(renameToFile)))
    {
     //success, verify now
      File destFile = new File(renameToFile);
      System.out.println("File Path : "+ destFile.getAbsolutePath());
      boolean renamed = false;
      while (TIMES_TO_RENAME_VERIFY-- > 0)             //5 times
      {
        try
        {
          Thread.sleep(RETRY_WAIT_TIME); //wait to confirm renaming        6000ms
          if (destFile.exists())
          {
            renamed = true;
            break;
          }
        }
        catch (InterruptedException ie)
        {
          log.exception("File rename wait exception ", ie);
        }
      }

      if (renamed)
        log.info("Successfully renamed the file " + filename +
                            " to append .load at the end");
      else
      {
        log.error("Failed to verify renamed file " + filename + ".load"); //this is the error I get
              return;
      }
    }
    else
    {
      log.error("Unable to rename the file " + filename + " to append .load at the end");
      return;
    }
Avatar of Vakils

ASKER

Sorry but I forgot to mention we are limited by Java 1.5
ASKER CERTIFIED SOLUTION
Avatar of Michael Fowler
Michael Fowler
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vakils

ASKER

Thanks guys for your efforts. It seems best solution is to move out of Java 1.5