• Post Reply Bookmark Topic Watch Topic
  • New Topic

Move file to new folder  RSS feed

 
Gerome Kawa
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Purpose of the program:
A thread should periodically check a folder and move the files in the folder to another new folder.

The existing folder name is : c:\Transmission
-Transmission (contains two files)
- Filename0.xml
- Filename1.xml
The destination folder is: c:\TransmissionResponded

I have written the following program but it comes up with the error saying
c:\TransmissionResponded created !
SendResponseThread to start ...
SendResponseThread to started ...
SendResponseThread running ...
copiedFileName.getName() :TransmissionResponded
destFile :c:\TransmissionResponded
destFile.canWrite() :true
f.getName() :FileName0.xml
File was not successfully moved !
copiedFileName.getName() :TransmissionResponded
destFile :c:\TransmissionResponded
destFile.canWrite() :true
f.getName() :FileName1.xml
File was not successfully moved !

********************************************************************

import java.io.*;
import java.util.Date;

/**
* Created by IntelliJ IDEA.
* User: kaghosh01
* Date: 24-Jan-2005
* Time: 10:50:01
* To change this template use Options | File Templates.
*/
public class SendResponseThread implements Runnable
{
Thread t;
String dirName;
File destFolder;
boolean dirCreated = false;
SendResponseThread()
{
dirName = "c:\\Transmission";
destFolder = new File("c:\\TransmissionResponded");

dirCreated = destFolder.mkdir();
if(dirCreated)
{
System.out.println( destFolder + " created !");
}else
{
System.out.println( "FATAL : " + destFolder + " did not create !");
}
System.out.println("SendResponseThread to start ...");
t = new Thread(this, "SendResponseThread");
t.start();
System.out.println("SendResponseThread to started ...");
}

public void run()
{
System.out.println("SendResponseThread running ...");
File f1 = new File(dirName);
File destFile;
String copiedFileName;
if(f1.isDirectory())
{
String s[] = f1.list();
boolean append = true;

for(int i=0; i < s.length; i++)
{
File f = new File(s[i]);
copiedFileName = destFolder.getName();
System.out.println("copiedFileName.getName() :" + copiedFileName);
destFile = new File("c:\\" + copiedFileName);

System.out.println("destFile :" + destFile);
System.out.println("destFile.canWrite() :" + destFile.canWrite());
System.out.println("f.getName() :" + f.getName());

// Move file to new directory
boolean success = f.renameTo(new File(destFile, f.getName()));
if (!success) {
System.out.println("File was not successfully moved !");
}
}

}
try
{
Thread.sleep(10000);
}catch(InterruptedException ie)
{
System.out.println("SendResponseThread interrupted." + ie.getMessage());
}
} // end run method
} // end class

********************************************************************

Can anyone point me the mistake why it is failing to move the file ?

Thanks
Kawa
 
Joseph Maddison
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've sometimes run into a situation where using a File object appears to somehow "lock" the file in question, but I'm not sure that's even the case here. You might try building the destination path + filename into a temporary String and use that instead of File.getName().

Here's an example that has worked for me... it successively appends "-a" to the filename untill it succeeds or reaches the limit of 10.


HTH,
jdmaddison
[ January 28, 2005: Message edited by: Joseph Maddison ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!