谁给个把文件copy到指定的路径的程序啊
谢了a

解决方案 »

  1.   

    import java.io.*;public class Test
    {
    public static void main(String[] args) throws Exception
    {
    File a = new File("sourceFile");
    System.out.println(a.getAbsolutePath());
    System.out.println(a.renameTo(new File("aimFile")));
    }
    }
      

  2.   


    晕,renameTo不知道什么意思啊
      

  3.   

    public static boolean copyTo(String src,String out)
    {
    try {
    File srcFile=new File(src);
    srcFile.renameTo(new File(out));
    } catch (Exception e) {
    return false;
    }
    return true;

    }呵呵 写个工具方法更好用
      

  4.   

    上面的不对吧,那怎么是文件复制呢?
    import java.io.*;public class Copy { public static void copyFile(File src, File dest) {
    FileOutputStream fos = null;
    FileInputStream fis = null;
    try{
    fis = new FileInputStream(src);
    fos = new FileOutputStream(dest);
    byte[] b = new byte[(int)src.length()];
    fis.read(b);
    fos.write(b);
    fos.flush();
    } catch(Exception e) {
    e.printStackTrace();
    } finally {
    try{
    if(null != fis) {
    fis.close();
    }
    if(null != fos) {
    fos.close();
    }
    } catch(Exception e) {
    e.printStackTrace();
    }
    }
    }

    public static void main(String[] args) {

    String src = args[0].replace("\\","\\\\");
    String dest = args[1].replace("\\","\\\\");
    copyFile(new File(src), new File(dest));
    }
    }
      

  5.   

    renameTo只是在同一个磁盘分区上才会成功,而且使用renameTo是移动而不是复制哦。    public static void copyFiles(File src, File dest) {
            // 系统的隐藏文件或是源文件不存在,不进行Copy
            if (src.isHidden() || !src.exists()) {
                return;
            }        // 对于文件夹需要递归的Copy
            if (src.isDirectory()) {
                if (!dest.exists()) {
                    dest.mkdirs();
                }
                File[] files = src.listFiles();
                for (int i = 0; i < files.length; i++) {
                    File destfile = new File(dest.getAbsolutePath() + File.separator + files[i].getName());
                    copyFiles(files[i], destfile);
                }
            } else {
                try {
                    File destfile = dest;
                    // 对于源文件是文件,而目标文件是目录的情况,将源文件以同名文件的形式Copy到目标目录下
                    if (dest.isDirectory()) {
                        destfile = new File(dest.getAbsolutePath() + File.separator + src.getName());
                    }
                    FileChannel sfc = new FileInputStream(src).getChannel();
                    FileChannel dfc = new FileOutputStream(destfile).getChannel();
                    sfc.transferTo(0, sfc.size(), dfc);                sfc.close();
                    dfc.close();
                    
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                } 
            }
        }
      

  6.   

    如果就是普通文件的话,这段就够了。而且这个速度比较快。                FileChannel sfc = new FileInputStream(src).getChannel();
                    FileChannel dfc = new FileOutputStream(destfile).getChannel();
                    sfc.transferTo(0, sfc.size(), dfc);                sfc.close();
                    dfc.close();