Vakils
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:
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;
}
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_TI ME); //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;
}
// 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_TI
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;
}
ASKER
Sorry but I forgot to mention we are limited by Java 1.5
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks guys for your efforts. It seems best solution is to move out of Java 1.5
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/