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

2021-04-26 19:28

阅读:529

标签:复制   input   java   try   stream   实现   com   imp   finally   

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();
                }
            }
        }
    }
}

注意:

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

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

标签:复制   input   java   try   stream   实现   com   imp   finally   

原文地址:https://www.cnblogs.com/elnimo/p/13251450.html


评论


亲,登录后才可以留言!