Quantcast
Viewing all articles
Browse latest Browse all 5930

IO流之字节流

学习导航

第一节:IO流之字符流http://blog.csdn.net/bobo8945510/article/details/52957339

IO流之基础讲解

一、什么是IO流?

Image may be NSFW.
Clik here to view.
这里写图片描述


二、字节和字符的区别!

Image may be NSFW.
Clik here to view.
这里写图片描述


三、字节流和字符流的区别?

Image may be NSFW.
Clik here to view.
这里写图片描述


四、IO流的分叉图!

Image may be NSFW.
Clik here to view.
这里写图片描述


五、字节流(FileInputStream和FileOutputStream)的使用!

Image may be NSFW.
Clik here to view.
这里写图片描述

五—-1:FileInputStream的使用及有缓存和没缓存的区别

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


public class TextRWByteStream01 {
    /*
     * 我们比较一下用缓存和没有缓冲的区别
     * 我们同样使用这个gi
     * */
    //-------------------------------------无缓存---------------------------------------
    public static void main(String[] args) {

        try {
            //读取我放在d://data//dongtai.gif的图片,使其转换为字节
            FileInputStream fin = new FileInputStream("d://data//dongtai.gif");

            //创建一个byte,长度为100.你设置的越少,访问磁盘的次数越多
            byte[] bytes = new byte[100];
            int num = 0;
            //开始读取我们获取当前时间
            long begin = System.currentTimeMillis();
            //判断是否读取到末尾
            while (fin.read(bytes)!=-1) {
                num++;
            }
            System.out.println("没有缓冲请求磁盘的次数:"+num);
            String end = System.currentTimeMillis()-begin+"ms";
            System.out.println("没有缓冲读取dongtai.gif图耗时:"+end);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    //-------------------------------------有缓存---------------------------------------
        System.out.println("==================打印数据分界线=====================");

        try {
            //读取我放在d://data//dongtai.gif的图片,使其转换为字节
            FileInputStream fin = new FileInputStream("d://data//dongtai.gif");
            BufferedInputStream bis = new BufferedInputStream(fin);
            //创建一个byte,长度为100.你设置的越少,访问磁盘的次数越多
            byte[] bytes = new byte[100];
            int num = 0;
            //开始读取我们获取当前时间
            long begin = System.currentTimeMillis();
            //判断是否读取到末尾
            while (bis.read(bytes)!=-1) {
                num++;

            }
            System.out.println("有缓冲请求磁盘的次数:"+num);
            String end = System.currentTimeMillis()-begin+"ms";
            System.out.println("有缓冲读取dongtai.gif图耗时:"+end);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

运行结果:可以清晰的看出,带缓冲的明显效率更高
Image may be NSFW.
Clik here to view.
这里写图片描述


五—-2:使用FileInputStream和FileOutputStream实现文件的复制,此demo没有用缓存

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


public class TextRWByteStream02 {
    /*
     * 运动字节流来复制一张图片
     * 1、我已经在我的D盘data文件夹下放置了一张gif图
     * 2、此demo没有用缓存
     * */
    public static void main(String[] args) {

        try {
            //读取我放在d://data//dongtai.gif的图片,使其转换为字节
            FileInputStream fin = new FileInputStream("d://data//dongtai.gif");
            //把我们获得的图片字节流,写成一个图片
            FileOutputStream outputStream = new FileOutputStream("d://data//newdongtai.gif");

            //创建一个byte,长度为1024.自己根据实际情况调整数组
            byte[] bytes = new byte[1024];
            //判断是否读取到末尾
            while (fin.read(bytes)!=-1) {
                //如果还有字节,就一直写入,到写入完
                outputStream.write(bytes);
            }
            outputStream.close();
            fin.close();
            System.out.println("复制成功");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

运行结果:成功
Image may be NSFW.
Clik here to view.
这里写图片描述


五—-3:使用FileInputStream和FileOutputStream实现文件的复制,用缓存

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


public class TextRWByteStream03 {
    /*
     * 运动字节流来复制一张图片---带缓冲
     * 1、我已经在我的D盘data文件夹下放置了一张gif图
     * 2、此demo有用缓存
     * */
    public static void main(String[] args) {

        try {
            //输入我放在d://data//dongtai.gif的图片,使其转换为字节,
            //添加缓存,并且还可以制定缓存区的大小。合理的添加缓存区的大小合理有效的节约读取时间。
            //由于我的这张图片不到1M。所以缓存区200就差不多了
            FileInputStream fin = new FileInputStream("d://data//dongtai.gif");
            BufferedInputStream bfin = new BufferedInputStream(fin,200);

            //输入流我们也添加缓存区
            FileOutputStream fout = new FileOutputStream("d://data//newdongtai.gif");
            BufferedOutputStream bfout = new BufferedOutputStream(fout);


            //创建一个byte,长度为1024.自己根据文件实际大小情况调整数组,
            //文件过大可以调大,文件过小就不要设置太大
            byte[] bytes = new byte[1024];
            //判断是否读取到末尾
            while (bfin.read(bytes)!=-1) {
                //如果还有字节,就一直写入,到写入完
                fout.write(bytes);
            }
            bfin.close();
            fin.close();
            bfout.close();
            fout.close();
            System.out.println("复制成功");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

运行结果:成功
Image may be NSFW.
Clik here to view.
这里写图片描述

demo地址:http://download.csdn.net/detail/bobo8945510/9667082

作者:bobo8945510 发表于2016/10/28 18:14:09 原文链接
阅读:44 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles