用字节流复制文本文档文件或者图片

package file;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class try2 {
    public static void main(String[] args) {

    String newFilePath="D:\\xk.jpg";//定义文件复制后的新地址
    String oldFilePath="C:\\Users\\Administrator\\Desktop\\cute.jpg";//定义文件的原地址
    copyFile(newFilePath, oldFilePath);调用复制方法
}

static void copyFile(String newFilePath,String oldFilePath)
{   
    FileInputStream fileInputStream=null;
    BufferedInputStream bufferedInputStream=null;
    FileOutputStream fileOutputStream=null;
    BufferedOutputStream bufferedOutputStream=null;//初始化输入输出流对象、缓冲区对象为全局变量、方便文件流最后调用关闭方法
    try {
        fileInputStream=new FileInputStream(oldFilePath);
        bufferedInputStream=new BufferedInputStream(fileInputStream);
//使用缓存区 无论读与写,先一次性塞满缓冲 区再让缓冲区执行读和写,提升效率直接进行读写,是操作的硬盘, 缓冲区存放是操作的内存。当然内存的执行速度是远远硬盘的执行cpu与硬盘打交道 远远低于cpu与内存的沟通。

        fileOutputStream=new FileOutputStream(newFilePath);
        bufferedOutputStream=new BufferedOutputStream(fileOutputStream);

        byte[] bytes=new byte[100];//定义一个字节数组用于存放文件流读取(吸入)和输出(吐出)时的字节
        int length=0;
        while((length=bufferedInputStream.read(bytes))!=-1)//当文件读取完毕时 read方法返回-1

        {
            bufferedOutputStream.write(bytes,0,length);//将文本文档或者图片读过来的字节直接输出到某个文件中 length有可能是100(字节数组长度),但也有可能是一个小于100的数,这里length是可变的,所以直接用有三个参数的write方法进行输出
        }
        bufferedOutputStream.flush();//这句话是一定要写的 是为了让缓冲区强制写入
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally {//finall中的语句是一定会执行的 通常用于文件流的关闭 或某个对象使用之后的关闭
        if(bufferedOutputStream!=null) {
        try {
            bufferedOutputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }   
        if(fileOutputStream!=null) {
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(bufferedInputStream!=null) {
            try {
                bufferedInputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        if(fileInputStream!=null) {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
    }
}}   

可以看到复制过来的图片(位于D盘根目录下) 文本文档的复制也可以使用
这里写图片描述
我只是想发张photo 哈哈哈哈哈哈 然后写写注释加深知识点记忆

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值