Java

一、文件复制练习

import java.io.*;

public class CopyStream {
    public void copyStream(File file,String path){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
           fis =  new FileInputStream(file);
           File file1 = new File(path+"\"+file.getName());
           fos = new FileOutputStream(file1);
           byte [] bytes = new byte[1024];
           int count = fis.read(bytes);
           while (count!=-1){
               fos.write(bytes,0,count);
               fos.flush();
               count = fis.read(bytes);
           }
            System.out.println("复制完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(fis!=null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fos!=null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

import java.io.File;

public class Test {
    public static void main(String[] args){
        CopyStream cs = new CopyStream();
        cs.copyStream(new File("F:\JavaTest\1\TestTest.txt"),"F:\JavaTest\2");
    }
}

 二、文件夹复制练习

import java.io.*;

    public void superCopy(File file,String newPath){

         String oldFilePath = file.getAbsolutePath();//获取源文件路径
         String newFilePath = newPath+oldFilePath.split(":")[1];//创建新的路径

         File newFile = new File(newFilePath);//创建新的对象

        File [] newFileArray = file.listFiles();//获取当前传递进来的file对象所有子元素

         if(newFileArray != null){//为空则是文件
             newFile.mkdir();
             System.out.println(newFile.getName()+"文件夹复制完毕");
             if(newFileArray.length!=0){
                 for(File f:newFileArray){
                     this.superCopy(f,newPath);
                 }
             }
         }else{//文件的复制
             FileInputStream fis = null;
             FileOutputStream fos = null;
             try {
                 fis  = new FileInputStream(file);
                 fos = new FileOutputStream(newFile);
                 byte [] bytes =new byte[1024];
                 int count = fis.read(bytes);
                 while (count != -1){
                     fos.write(bytes,0,count);
                     fos.flush();
                     count = fis.read(bytes);
                 }
                 System.out.println(newFile.getName()+"文件复制完毕");
             } catch (IOException e) {
                 e.printStackTrace();
             }finally {
                 if(fis == null){
                     try {
                         fis.close();
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
                 }
                 if(fos == null){
                     try {
                         fos.close();
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
                 }
             }
         }
    }
}

public class Test {
    public static void main(String[] args){
        CopyStream cs = new CopyStream();
        cs.superCopy(new File("F:\JavaTest"),"F:\JavaTest");
    }
}
原文地址:https://www.cnblogs.com/yyanghang/p/11254651.html