现在要求是从现有的文件中,通过指定FileList将现有文件拷贝出来。
   如:
   c:\study\1.txt
   c:\study\2.txt
   c:\study\abc\3.txt
  到
   d:\study\1.txt
   d:\study\2.txt
   d:\study\abc\3.txt c:中可能还有很多别的文件!

解决方案 »

  1.   

    File folder = new File("c:/study");String[] files = folder.list()FIle f = new File(files[i]);
    f.isFile();  ===> bool v;//这里如果是dir的话,要递归一下。copyFile//要自己实现,Java,Python,Ruby都没有提供copy文件的API,(Ruby的copy file还是字符流读取实现的,都是没有真正系统级别的CopyFile的)剩下的就简单了。
    copyFile用In和Out流。
      

  2.   

    import java.io.*;public class FileCopy{
    public static void main(String[] args){
    if(args.length != 2){
    System.err.println("Usage:java FileCopy <source> <destination>");
    }else{
    try{
    copy(args[0],args[1]);
    }
    catch(IOException e){
    System.err.println(e.getMessage());
    }
    }
    }

    private static void copy(String from_name,String to_name) throws IOException{
    File from_file = new File(from_name);
    File to_file = new File(to_name);

    if(!from_file.exists())
    abort("no such source file:" + from_name);
    if(!from_file.isFile())
    abort("can't copy directory:"+ from_name);
    if(!from_file.canRead())
    abort("source file is unreadable:"+ from_name);

    if(to_file.isDirectory())
    to_file = new File(to_file,from_file.getName());

    if(to_file.exists()){
    if(!to_file.canWrite()){
    abort("destination file is unwirteable"+to_name);
    }
    System.out.println("Overwirte existing file"+to_file.getName()+"?(Y/N):");
    System.out.flush();
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    String response = in.readLine();
    if(!response.equals("Y") && !response.equals("y"))
    abort("existing file was not overwritten");
    }
    else{
    String parent = to_file.getParent();
    if(parent==null){
    parent = System.getProperty("user.dir");
    }

    File dir = new File(parent);
    if (!dir.exists( ))
                    abort("destination directory doesn't exist: "+parent);
                if (dir.isFile( ))
                    abort("destination is not a directory: " + parent);
                if (!dir.canWrite( ))
                    abort("destination directory is unwriteable: " + parent);
    }

    FileInputStream from = null;
    FileOutputStream to = null;

    try{
    from = new FileInputStream(from_file);
    to = new FileOutputStream(to_file);
    byte[] buffer = new byte[4096];
    int bytes_read;

    while((bytes_read = from.read(buffer))!=-1)
    to.write(buffer,0,bytes_read);
    }
    finally{
    if(from != null) try{from.close();}catch(IOException e){;}
    if(to != null) try{to.close();}catch(IOException e){;}
    }
    }
    private static void abort(String msg) throws IOException{
    throw new IOException(msg);
    }
    }
      

  3.   

    public class MergeSource { public static void copyFile(File from, File to) throws IOException { int byteRead = 0;
    InputStream inStream = new FileInputStream(from);
    FileOutputStream foStream = new FileOutputStream(to);
    BufferedOutputStream boStream = new BufferedOutputStream(foStream);
    byte[] buffer = new byte[1440];
    while ((byteRead = inStream.read(buffer)) != -1) {
    boStream.write(buffer, 0, byteRead);
    boStream.flush();
    }
    inStream.close();
    foStream.close();
    } public static void main(String[] args) throws IOException {
    //get changeList
    File fileList = new File("d://FileList.txt"); InputStream in = new FileInputStream(fileList);
    DataInputStream dataInput = new DataInputStream(in); String path;
    while ((path = dataInput.readLine()) != null) { if (path.length() > 0) {
    if (path.lastIndexOf(":") == -1) {
    String parentPath = System.getProperty("user.dir");
    path = parentPath + "\\" + path;

    File src = new File(path);
    String CanonicalPath = src.getCanonicalPath();
    String newPath = "d"
    + CanonicalPath
    .substring(1, CanonicalPath.length());
    File dst = new File(newPath);
    if (dst.exists()) {
    System.out.println(newPath + " is Alreadly exist");
    continue;
    }
    String parent = dst.getParent();
    File pathFile = new File(parent);
    pathFile.mkdirs();
    copyFile(src, dst);
    }
    }
    }}
      

  4.   

    不好意识,没有说明白。首先你必须通过别的工具得出你修改过的文件!存入fileList