The Artima Developer Community
Sponsored Link

Java Answers Forum
Help me for my program regarding with File listing, checking and copying

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
poephyu

Posts: 1
Nickname: poe
Registered: Oct, 2003

Help me for my program regarding with File listing, checking and copying Posted: Oct 14, 2003 1:55 AM
Reply to this message Reply
Advertisement
This is my program for file listing,checking and copying.

import java.io.*;
import java.nio.channels.*;
import java.util.*;

public class FileChecker implements Runnable{

protected File sDir; //source directory
protected File dDir; //destination directory
protected Vector fileListHolder;
protected String newFileName;
protected String s_dir;
protected String d_dir;

public FileChecker(){

d_dir = "C:/Program Files/Apache Group/Tomcat 4.1/webapps/examples/IXOS/userfolder";

s_dir = "C:/Program Files/Apache Group/Tomcat 4.1/webapps/examples/IXOS/conso";

fileListHolder = new Vector();
newFileName = new String();

}

public String checkNewFile(){

boolean b = false;
sDir = new File(s_dir);
newFileName = new String();

try{

String[] fileList = sDir.list();
if(fileList.length == 0){
fileListHolder.removeAllElements();

}

for(int i=0; i<fileList.length; i++)
{
b = fileListHolder.contains(fileList);
if(b == false){

fileListHolder.add(fileList);
newFileName = fileList;

}

else if(b == true){
newFileName = null;

}
}
}
catch(Exception e){
e.getMessage();
}
return newFileName;
}

public void copyFile(String fileToCopy){

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();

Syst em.out.println("Source file :"+source);
System.out.println("Destination File:"+destination);

}

source = null;
s = null;
s1 = null;
s2 = null;
destination = null;

}

catch (Exception e){ // handle any IOException

//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.

Topic: Popup Previous Topic   Next Topic Topic: HELP!! first words capitalized

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use