|
Here you go, I have more file operations but you can do it some more simpler... Just check it out.
public class TestMaruti implements FilenameFilter {
int iCount = 0;
public String str1 = null;
public void process(String Filter,String FilePath) throws Exception
{
str1=Filter;
File file = new File(FilePath);
String [] str = file.list(this);
File file1 = new File(FilePath+"/"+Filter);
if(!file1.isDirectory())
{
System.out.println(iCount++);
file1.mkdir();
for(int i=0;i<str.length;i++)
{
System.out.println("Writing the file "+str[i]+"....");
File file2 = new File(FilePath+"/"+str[i]);
File file3 = new File(FilePath+"/"+Filter+"/"+str[i]);
moveFiles(file2,file3);
System.out.println("Completed");
}
}
//file.mkdir();
}
public boolean accept(File dir,String s)
{
if(s.startsWith(str1))
{
return true;
}
else
{
return false;
}
}
public static void main (String args[]) throws Exception
{
String path="D:/Maruti/Test";
TestMaruti tm = new TestMaruti();
File allFiles = new File(path);
String [] strFiles = allFiles.list();
for(int j=0;j<strFiles.length;j++)
{
String flName = strFiles[j];
flName=flName.substring(0,flName.indexOf("."));
tm.process(flName,path);
}
}
public void moveFiles(File source,File destination) throws Exception
{
InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(destination);
byte [] buf = new byte[4096];
int len;
while((len=in.read(buf)) > 0 )
{
out.write(buf,0,len);
}
in.close();
out.close();
source.delete();
}
}
Please let me know incase of questions.
|