day24

复制目录到指定路径

file-App下的src目录复制到 D:/aaa
public static void copy(File src, File dest) throws IOException {
    //0. 创建出目标路径
    if (!dest.exists()){
        dest.mkdirs();
    }
    //0.0 判断dest是否存在,如果不存在,创建爱你出来,不能复制,结束方法
           // 0.00即使原本存在文件名叫this.dest 也可能是一个文件而不是文件夹
    if (dest.isFile()){
        System.out.println("要复制的路径是文件不是文件夹");
        return;
    }

    //1. 获取源目录的子项
    File files[] =  src.listFiles();
    //2. 判断子项是否是文件
    if (files == null){
        System.out.println("没有可复制的文件");
        return;
    }

    //2.1 遍历files获取每一个子项
    for (File file : files) {
        if (file.isFile()){
            FileInputStream fis = new FileInputStream(src);
            FileOutputStream fos = new FileOutputStream(new File(dest,file.getName()));

            byte bytes[] = new byte[8*1024];
            int a;
            while ((a=fis.read(bytes)) != -1){
                fos.write(bytes,0,a);
            }
            fos.close();
            fis.close();
            //2.2判断file是否是文件,是文件,则复制,不是文件则递归            
        }else {
            copy(file,new File(dest,file.getName()));
        }
    }
}



 输入字符串,使用流写出

需要转换字符为字节码数组 

day24_第1张图片



     五种流的复制方法

package com.oracle.file.day24;

import java.io.*;

/**
 * 五种复制方式
 */
public class Test {
    public static void main(String[] args) {

    }
    public static void copy011() throws IOException {
        FileInputStream fis = new FileInputStream("file-APP\\fos.txt");
        FileOutputStream fos = new FileOutputStream("fos1.txt");
        int a;
        while ((a=fis.read())!=-1){
            fos.write(a);
        }
        fos.close();
        fis.close();
    }
    public static void copy012() throws IOException{
        FileInputStream fis = new FileInputStream("file-APP\\fos.txt");
        FileOutputStream fos = new FileOutputStream("fos1.txt");
        int a;
        byte bytes[] = new byte[8*1024];
        while ((a=fis.read(bytes))!=-1){
            fos.write(bytes,0,a);
        }
        fos.close();
        fis.close();
    }
    public static void copy013() throws IOException{
        FileInputStream fis = new FileInputStream("file-APP\\fos.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream("fos1.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int a;
//        byte bytes[] = new byte[8*1024];
        while ((a=bis.read())!=-1){
            bos.write(a);
        }

        bos.close();
        bis.close();

    }
    public static void copy014() throws IOException{
        FileInputStream fis = new FileInputStream("file-APP\\fos.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream("fos1.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int a;
        byte bytes[] = new byte[8*1024];
        while ((a=bis.read(bytes))!=-1){
            bos.write(bytes,0,a);
        }

        bos.close();
        bis.close();

    }
    public static void copy015() throws IOException{
        FileInputStream fis = new FileInputStream("file-APP\\fos.txt");
        BufferedInputStream bis = new BufferedInputStream(fis,8*1024);
        FileOutputStream fos = new FileOutputStream("fos1.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos,8*1024);
        int a;
        byte bytes[] = new byte[8*1024];
        while ((a=bis.read(bytes))!=-1){
            bos.write(bytes,0,a);
        }

        bos.close();
        bis.close();
    }


}

 

 



将对象转换为字节对象

day24_第2张图片

 将对象的字节文件转换为对象

day24_第3张图片

 

 

你可能感兴趣的:(java,前端,git)