I have a situation in which I want to copy the images from a directory to another directory. Also, I want the copied images to be stored with the different names. This is what I have done:
public static void main(String []args)
{
File source = new File("/home/src");
//rest of the code
File[] listOfFiles = source.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
File f = new File("/home/src/"+listOfFiles[i].getName()); //I want to copy all the images from src to dest
boolean b1 = f.renameTo(new File("/home/dest/"+i+".jpg"));
//rest of the code
}
}
Now, the problem is, when I'm running this code, the function renameTo()
is renaming the images properly, but it is moving the images from "src" directory to "dest" directory. I don't want that to happen. I want all the images in "src" directory intact. So to achieve this, what I'm currently doing is, I'm using this code as it is and at the end I'm copying all the images back from "dest" to "src" using FileUtils.copyDirectory()
. A lot of googling also didn't help. Is there any way of achieving it directly? Or I need to proceed with what I'm currently doing?