Thursday, March 14, 2013

moving specific files to other destination directory

Suppose sorce directory having many fies like some of them are .txt,.doc,.xml,.....etc
now i want to move a the .txt files to one seperate folder and n the same way other files also.

import java.io.File;
//commons-io2.0.1.jar for FileUtils,FilenameUtils
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;


public class Filesamp {
public static void main(String a[])throws Exception
{
File srcFolder = new File("L:/resumes");
File processedDir = new File("L:/destination");
File shdir = new File("L:/destination/shdir");
File afile[];
intj = (afile = srcFolder.listFiles()).length;
for(inti = 0; i < j; i++)
{
File fileEntry = afile[i];
String fileName = fileEntry.getName();
System.out.println(fileName);
//moving specific files to other destination directory
if("doc".equalsIgnoreCase(FilenameUtils.getExtension(fileName)))
{

FileUtils.moveFileToDirectory(fileEntry, processedDir, true);
}
if("txt".equalsIgnoreCase(FilenameUtils.getExtension(fileName)))
{

FileUtils.moveFileToDirectory(fileEntry, shdir, true);
}



}
}
}


No comments:

Post a Comment