在copyFile时,如果先写了:“File newFile = new File(newPath)”,
然后再copyFile(oldPath, newPath),在这个copyFile方法当中,如果将输出流(new FileOutputStream(newPath))关闭,那这个copyFile方法结束时,那个newFile会被删掉。求解!

解决方案 »

  1.   

    package pra.io;import java.io.*; 
    public class TestCopyFile{ 
        public static void main(String[] args) throws IOException { 
            if(args.length!=2){ 
                System.out.println("命令行参数输入有误,请检查"); 
                System.exit(1); 
            } 
            File file1=new File(args[0]); 
            File file2=new File(args[1]); 
              
            if(!file1.exists()){ 
                System.out.println("被复制的文件不存在"); 
                System.exit(1); 
            } 
            InputStream input=new FileInputStream(file1); 
            OutputStream output=new FileOutputStream(file2); 
            if((input!=null)&&(output!=null)){ 
                int temp=0; 
                while((temp=input.read())!=(-1)){ 
                    output.write(temp); 
                } 
            } 
            input.close(); 
            output.close();  
        } 

    文件名我是传参数的,但是应该和你说的情况差不多,但是我的文件没被删掉啊
      

  2.   

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
     
    public class CTest {
     
        /**
         * @param args
         */
        public static void main(String[] args) {
     
            String filePath = "G:/12/path";
            String copypath="G:/12/copypath";
            File file = new File(filePath);
            if(!file.exists()){
                System.out.println("文件不存在");
            }
            if(!new File(copypath).exists()){
                new File(copypath).mkdirs();
                System.out.println("文件以创建");
            }
            //获取文件下的文件或目录
            File[] filelist = file.listFiles();
            for(int i=0;i<filelist.length;i++){
                if(filelist[i].isFile()){
                    copyFile(filelist[i], copypath+file.separator+filelist[i].getName());
                }else{
                    String target=copypath+file.separator+filelist[i].getName();
                    copyDirecty(filelist[i],target);
                }
            }
             
        }
     
        /**
         * 复制文件夹
         * @param filelist
         * @param copypath
         */
        private static void copyDirecty(File filelist, String path) {
            (new File(path)).mkdirs();
            //判断源文件目录下的文件/文件夹
            File[] list = filelist.listFiles();
            for(int i=0;i<list.length;i++){
                if(list[i].isFile()){
                    copyFile(list[i], path+"/"+list[i].getName());
                }else{
                    //目标文件目录
                    String target=path+"/"+list[i].getName();
                    copyDirecty(list[i],target);
                }
            }
        }
     
        /**
         * 复制文件
         * @param filelist
         * @param copypath
         */
        private static void copyFile(File filelist, String copypath) {
            try {
                FileInputStream in=new FileInputStream(filelist);
                FileOutputStream out=new FileOutputStream(copypath);
                BufferedInputStream bin=new BufferedInputStream(in);
                BufferedOutputStream bout=new BufferedOutputStream(out);
                int len=0;
                byte[] b = new byte[8*1024];
                while(bin.read(b)!=-1){
                    bout.write(b, 0, len);
                }
                bout.flush();
                bout.close();
                bin.close();
                out.close();
                in.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
     
    }
      

  3.   

    public void copyFileToDest(File srcFile, String tarPath) throws IOException {
    InputStream is = new FileInputStream(srcFile);
    FileOutputStream os = new FileOutputStream(new File(tarPath, srcFile.getName()));
    int size = is.available();
    byte[] bytes = new byte[size];
    is.read(bytes);
    os.write(bytes);
    os.close();
    is.close();
    }简单,直接;
      

  4.   

    ls 直接的 1.7 可以使用 java.nio.file.Files的copy方法。
    之前使用 FileChannel的transferFrom/transferTo方法。
      

  5.   

    楼主的代码我懒得看,我在想你是不是没flush