栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

输入输出流拷贝文件+资源关闭

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

输入输出流拷贝文件+资源关闭

文件拷贝
package jess.day13;

import org.junit.Test;

import java.io.*;


public class CopyStream {
    
    public void copyFileStream(String inPath, String outPath) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        // 容器 每次传输1K
        byte[] b = new byte[1024];
        int len = 0;
        try {
            // 构建输入输出对象
            inputStream = new FileInputStream(inPath);
            outputStream = new FileOutputStream(outPath);
            // 读取文件
            while ((len = inputStream.read(b)) != -1) {
                // 写入文件 每次从b写入 个从0 写到len
                outputStream.write(b, 0, len);
                System.out.println(len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 关闭资源
        finally {
            // 判断是否为空
            if (inputStream != null) {
                try {
                    // 关闭输入流
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void copyTest() {
        String inPath = "E:\谷歌下载\蓝底二寸.png";
        String outPath = "D:\360软件管家\蓝底二寸.png";
        copyFileStream(inPath, outPath);
    }
}

结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/295206.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号