38、使用IO流进行文件拷贝

使用IO流进行文件拷贝

需求:在项目的根目录里面创建一个java.txt的文件,然后将这个文件拷贝到file文件夹里面并且重命名为good.txt文件
先以流的方式将java.txt文件读取到内存中,然后再以流的方式将内存中的内容写出到硬盘里面

package com.sutaoyu.IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class IO_test_1 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("java.txt");
            fos = new FileOutputStream("file" + File.separator + "good.txt");
            
            byte[] arr = new byte[1024];
            int length;
            while((length = fis.read(arr)) != -1) {
                fos.write(arr,0,length);
            }
        }catch(FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            try {
                fis.close();
                fos.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}

使用缓冲流进行文件拷贝

Java中提供了BufferedInputStream和BufferedOutputStream缓冲流用来读取和写出, BufferedInputStream读取时会创建一个长度为8192的byte类型数组,程序一次读取8192个字节数据到数组中 使用缓冲流之后就不用再自定义byte类型数组了。

package com.sutaoyu.Buffer;

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 Buffer_test_1 {
    public static void main(String[] args) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
             //使用缓冲流装饰一下
            bis = new BufferedInputStream(new FileInputStream("java.txt"));
            bos = new BufferedOutputStream(new FileOutputStream("file" + File.separator + "good.txt"));
            
            int temp;
            while((temp = bis.read()) != -1) {
                bos.write(temp);
            }
            
        }catch(FileNotFoundException e) {
            e.printStackTrace();
            
        }catch(IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bis.close();
                bos.close();
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
    }
}

使用自定义数组和buffer的图解

使用自定义数组:

使用Buffer缓冲流:

 两者在效率上区别不大,其实java提供的BufferedInputStream和BufferedOutputStream底层使用的也是byte类型的数组,开发中这两种方式都可以使用。

jdk7的新写法

在jdk7中新加入了AutoCloseable接口,IO流中的类都实现了这个接口,这样在读取或者写出操作结束之后,系统会自动close相关资源,开发者不需要再手动close了

package com.sutaoyu.Buffer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Buffer_test_2 {
    
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("monkey.txt");
            fos = new FileOutputStream("rabbit.txt");
            
            int length;
            while((length = fis.read()) != -1) {
                fos.write(length);
            }
        }catch(FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
}
}
原文地址:https://www.cnblogs.com/zhuifeng-mayi/p/10140090.html