博客
关于我
字节流练习:图片复制
阅读量:508 次
发布时间:2019-03-07

本文共 1311 字,大约阅读时间需要 4 分钟。

文件复制练习:一读一写

    明确:

        数据源: c:\\1.jpg
        数据的目的地: d:\\1.jpg

    文件复制的步骤:

        1.创建一个字节输入流对象,构造方法中绑定要读取的数据源
        2.创建一个字节输出流对象,构造方法中绑定要写入的目的地
        3.使用字节输入流对象中的方法read读取文件
        4.使用字节输出流中的方法write,把读取到的字节写入到目的地的文件中
        5.释放资源

package com.itheima.demo03.CopyFile;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class Demo01CopyFile {    public static void main(String[] args) throws IOException {        long s = System.currentTimeMillis();        //1.创建一个字节输入流对象,构造方法中绑定要读取的数据源        FileInputStream fis = new FileInputStream("c:\\1.jpg");        //2.创建一个字节输出流对象,构造方法中绑定要写入的目的地        FileOutputStream fos = new FileOutputStream("d:\\1.jpg");        //一次读取一个字节写入一个字节的方式        //3.使用字节输入流对象中的方法read读取文件        /*int len = 0;        while((len = fis.read())!=-1){            //4.使用字节输出流中的方法write,把读取到的字节写入到目的地的文件中            fos.write(len);        }*/        //使用数组缓冲读取多个字节,写入多个字节        byte[] bytes = new byte[1024];        //3.使用字节输入流对象中的方法read读取文件        int len = 0;//每次读取的有效字节个数        while((len = fis.read(bytes))!= -1){            //4.使用字节输出流中的方法write,把读取到的字节写入到目的地的文件中            fos.write(bytes,0,len);        }        //5.释放资源(先关写的,后关闭读的;如果写完了,肯定读取完毕了)        fos.close();        fis.close();        long e = System.currentTimeMillis();        System.out.println("复制文件共耗时:"+(e-s)+"毫秒");    }}

 

转载地址:http://rtwnz.baihongyu.com/

你可能感兴趣的文章
Nginx+Django-Python+BPMN-JS的整合工作流实战项目
查看>>
Nginx+Keepalived+LVS集群实战
查看>>
Nginx+Keepalived实现简单版高可用主备切换
查看>>
Nginx+Lua 开发高性能Web应用实战
查看>>
nginx+mysql+redis+mongdb+rabbitmq 自动化部署脚本
查看>>
nginx+php的搭建
查看>>
Nginx+Redis+Ehcache:大型高并发与高可用的三层缓存架构总结
查看>>
nginx+tomcat+memcached
查看>>
nginx+tomcat单个域名及多个域名配置
查看>>
Nginx+Tomcat实现动静分离
查看>>
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>
nginx+vsftp搭建图片服务器
查看>>
Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
nginx-vts + prometheus 监控nginx
查看>>
Nginx/Apache反向代理
查看>>
Nginx: 413 – Request Entity Too Large Error and Solution
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
查看>>
nginx: [error] open() “/usr/local/nginx/logs/nginx.pid“ failed (2: No such file or directory)
查看>>