Discussion Locked

moving files in java

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I want to move files from one folder to another in Java. I used the renameTo method in the File class but that doesnt helps me...
Am posting my code here, can some one look at it and tell me whats wring in it and also is there an efficient way to move files in Java across folders...

public boolean moveFiles(String source, String destination)
{
//This method moves the files from one directory to another
File sourceDir = new File(source.trim());
File destinationDir = new File(destination.trim());

boolean created = true;
if(!destinationDir.exists())
{
//Create that directory

created = destinationDir.mkdir();
System.out.println("The directory created " + created);

//do the moving of files here

File [] tmp = sourceDir.listFiles();
for(int i =0;i

3 replies

by sgovindu

renameTo seems not to work with driectories. In your loop of renameTo try the following:

for(int i = 0; i < tmp.length; i++)
{
File newFile = new File(destinationDir, tmp[i].getName());
tmp[i].renameTo(newFile);
}

This discussion is closed to new replies.