使用java的IO复制图片文件

使用java的IO复制图片文件

详细步骤都在代码的注释中!

java代码如下:

package com.lbl.io;

import org.junit.Test;

import java.io.*;

public class IOTest {
    
    

    @Test
    public void IOTest() throws IOException {
    
    
        //复制web目录下,download目录下的a.jpg图片文件
        //1.首先明确,我们是复制,所以即要输出流又要输入流,又因为是图片,所以选择字节流
        // 声明输入流
        InputStream inputStream = null;
        // 声明输出流
        OutputStream outputStream = null;
        // 被复制的文件
        File fromFile = new File("F:/project/Day03/web/download/a.jpg");
        // 复制出来的文件
        File toFile = new File("F:/project/Day03/web/copy/a.jpg");
        //想要实现边读边写,所以要创建缓冲区
        byte[] buffer = new byte[1024];        //我这边用一个1024大小的字符数组作为缓冲区
        int len = 0;
        inputStream=new FileInputStream(fromFile);
        outputStream=new FileOutputStream(toFile);
        while ((len = inputStream.read(buffer))!=-1) {
    
    
            outputStream.write(buffer,0,len);
        }

        //关闭资源
        outputStream.close();
        inputStream.close();
    }
}

目录结构如下:

目录结构

猜你喜欢

转载自blog.csdn.net/qq_37924905/article/details/108500957
今日推荐