Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Move file and rename if it is not being accessed by other process

Status
Not open for further replies.

aswolff

Programmer
Jul 31, 2006
100
US
Hi.

I need to write a program (java) that periodically runs and checks a directory for files and for each file determine if any processes are writing to it. If not then move the file.

Can somebody point me in the right direction as to a class/method that might do this? This will be implemented on Sun Solaris 9. Java 1.4.2_10.

Thanks.
 
You cannot tell in java if a file is being accessed by another process - nor will an exception be thrown if you attempt to rename a file and it is still being accessed by another process.

The only way you can do is this :


Code:
String filename = "bla.txt";
String newfilename = "bla_renamed.txt";

File f = new File(filename);
File newf = new File(newfilename);

f.renameTo(newf);

if (new File(filename).exists()) {
  throw new IOException("Could not rename file - either the file permissions are incorrect, or it is being used by another process");
} else {
   System.out.println("rename of file succeeded");
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top