Solved

Rename file in Java

Posted on 2014-11-19
6
420 Views
Last Modified: 2014-11-20
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;
    }
0
Comment
Question by:vakils
6 Comments
 
LVL 23

Expert Comment

by:Michael74
ID: 40454150
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/
0
 
LVL 21

Expert Comment

by:Amitkumar Panchal
ID: 40454152
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;
    }
0
 

Author Comment

by:vakils
ID: 40454235
Sorry but I forgot to mention we are limited by Java 1.5
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 23

Accepted Solution

by:
Michael74 earned 250 total points
ID: 40454245
In that case it could be related to Bug ID 6213298
A DESCRIPTION OF THE PROBLEM :
After releasing a File object, another File object for the same file name cannot be renamed immediately.  Here's my work-around that usually works but still sometimes fails.
http://bugs.java.com/view_bug.do?bug_id=6213298

The link above has a work around but the solutions I posted should still work
0
 
LVL 14

Assisted Solution

by:Tommy Braas
Tommy Braas earned 250 total points
ID: 40455003
Use Runtime.exec(...)or  Apache Commons Exec to execute the shell command "mv" to move/rename your file. Keep in mind that this way of renaming/moving a file is not cross-platform.
0
 

Author Closing Comment

by:vakils
ID: 40456614
Thanks guys for your efforts. It seems best solution is to move out of Java 1.5
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This video teaches viewers about errors in exception handling.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question