JAVA基础复习一 字节输入、输出流整合(实现图片复制)

package com.winson.iotest;

import org.junit.Test;

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

/**
 * @description:输入字节流、输出字节流整合(实现图片的复制)
 * @date: 2020/7/5 19:41
 * @author: winson
 */
public class FileInputStreamFileOutputStreamTest {

    @Test
    public void test1() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            File file = new File("ludashi.jpg");
            File file1 = new File("ludashi_out.jpg");
            fileInputStream = new FileInputStream(file);
            fileOutputStream = new FileOutputStream(file1);
            byte[] bytes = new byte[1024];
            int len;
            while ((len = fileInputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

注意:

字符流不能处理非文字文件(带图片、视频等文件)

posted @ 2020-07-05 19:59  温森  阅读(209)  评论(0编辑  收藏  举报