java.io.File的API没有提供copy的接口,你可以自己实现

解决方案 »

  1.   

    用执行外部命令的方式好了,“copy”
      

  2.   

    import java.io.*;public class CopyFile
    {
    public 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 file:" + from_name);
    if(!from_file.isFile()) abort("a directory:" + from_name);
    if(!from_file.canRead()) abort("can't read:" + 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 unwriteable:" + to_name);
    System.out.print("Overwrite existing file:" + to_file.getName() + "? (Y/N):");
    System.out.flush();
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    String res=in.readLine();
    if(!res.equals("Y")&& !res.equals("y")) abort("not overwrite:" + to_name);
    }else{
    String parent=to_file.getParent();
    if(parent==null) parent=System.getProperty("user.dir");
    File dir=new File(parent);
    if(!dir.exists()) abort("destination file is not exist:" + parent);
    if(dir.isFile()) abort("destination file is not a directory:" + parent);
    if(!dir.canWrite()) abort("can't write:" + 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) {;}
    }

    }

    public static void abort(String msg) throws IOException
    {
    throw new IOException("filecopy:" + msg);
    }

    public static void main(String args[]) throws IOException
    {
    CopyFile cp=new CopyFile();
    cp.copy(args[0],args[1]);
    }
    }
      

  3.   

    这是一个copy 文件的java程序
    试试看