boolean b = false; String source = new String(); String s = new String(); String s1 = new String(); String s2 = new String(); String destination = new String();
try{
source = s_dir + "/" + fileToCopy; int i = fileToCopy.lastIndexOf(".");
s = fileToCopy.substring(0,i); //extract filename without dot e.g. "filename.java" to "filename"
s1 = fileToCopy.substring(0,4); //allow only four character for folder name
s2 = d_dir +"/"+s1;
File dDir = new File(s2);
b = dDir.isDirectory();
if(b == true){
destination = s2 + "/"+ fileToCopy; FileChannel sourceChannel = new FileInputStream(source).getChannel(); FileChannel destinationChannel = new FileOutputStream(destination).getChannel(); // Copy source file to destination file destinationChannel.transferFrom (sourceChannel, 0, sourceChannel.size()); sourceChannel.close(); destinationChannel.close();
//just bypass the errors e.getMessage(); return; } }
public void run(){ for(;;){ newFileName = checkNewFile(); if(newFileName != null){ System.out.println("Found new File:"+ newFileName); copyFile(newFileName);
} } }
public static void main(String [] arg){ FileChecker fc = new FileChecker(); Thread fcThread = new Thread(fc); fcThread.start();
} }
That program can copy file name same name as the folder name.I compare at the first four character, e.g,If the file name is "davider.xls",this will be copied to "davi" folder.
But the problem is I can't copy more than one file to the folder.That's mean if the "davider.xls" file is exist in the "davi" folder, I can't copy another file "daviss.doc" to that folder.
The another problem is I can't put the files randomly to source folder.I have to put according to alphabet.For example,"christ.xls","davider.doc","nicklace.xls",etc.If i put "nicklace,xls" file first and put "davider.doc" , "christ.xls",It cannot copy to respective folders of userfolder.
Then,the last one is It cannot overwrite the file. If you got the idea, please change my code to get my point and reply it to me.
Please help me to solve my problems.It make me very headache.I'll appreciate ur help.