java基础io流——OutputStream和InputStream的故事(温故知新)

io流概述:

IO流用来处理设备之间的数据传输,上传文件和下载文件,Java对数据的操作是通过流的方式,Java用于操作流的对象都在IO包中。

IO流分类

按照数据流向

输入流 读入数据

输出流 写出数据

按照数据类型

字节流

字符流

什么情况下使用哪种流呢?

如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流,其他用字节流。

如果你什么都不知道,就用字节流。

IO流常用基类

字节流的抽象基类:

InputStream ,OutputStream。

字符流的抽象基类:

Reader , Writer。

注:
由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀。
如:InputStream的子类FileInputStream。
如:Reader的子类FileReader。

OutputStream的子类FileOutputStream

构造方法:

FileOutputStream(File file)

FileOutputStream(String name)

推荐第二种构造方法:

FileOutputStream outputStream = new FileOutputStream("a.txt");

创建字节输出流对象了做了几件事情:

A:调用系统功能去创建文件
B:创建outputStream对象
C:把foutputStream对象指向这个文件
通过字节输出流写出数据到文本
public void write(int b)
public void write(byte[] b)
public void write(byte[] b,int off,int len)

从方法中可看出,只能通过字节写出

outputStream.write("hello".getBytes()); 文本中出现hello
outputStream.write(96)  //文本中出现 a

byte[] bys={97,98,99,100,101};
outputStream.write(bys,1,3); 文本中出现bcd

如此写出,文本中数据不会换行,不会追加,每次写出都是覆盖原来。

追加:
FileOutputStream outputStream = new FileOutputStream("a.txt",true);
//第二个参数true设置为可追加。
换行 \n\r :
for (int i = 0; i <5 ; i++) {
    outputStream.write("hello".getBytes());
    outputStream.write("\n\r".getBytes());
    }

注:用完流一定要记得关闭。

outputStream.close();

完整示例:

package io2;

import java.io.FileOutputStream;
import java.io.IOException;
/**
 * new FileOutputStream("a.txt",true);  第二个参数true,设置为写入的数据拼接在尾部
 * \n\r 换行
 * write(bys,1,3);  写入字节数组
 */
public class out {
    public static void main(String args[]){
        FileOutputStream outputStream = null;
        try {
            //FileOutputStream fos = new FileOutputStream(file);
            outputStream = new FileOutputStream("a.txt",true);
            /*
             * 创建字节输出流对象了做了几件事情:
             * A:调用系统功能去创建文件
             * B:创建outputStream对象
             * C:把foutputStream对象指向这个文件
             */

//            for (int i = 0; i <5 ; i++) {
//                outputStream.write("hello".getBytes());
//                outputStream.write("\n\r".getBytes());
//            }
            byte[] bys={97,98,99,100,101};
            outputStream.write(bys,1,3);
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}

InputStream的子类FileInputStream

FileInputStream的构造方法
FileInputStream(File file)
FileInputStream(String name)

推荐第二种构造方法:

 FileInputStream inputStream = new FileInputStream("a.txt");
把刚才写的数据现在读取到控制台:
public int read()
public int read(byte[] b)

第一个read是读一个字节,第二个read是读一个字节数组。

//读一个字节
int by = 0;
while ((by=inputStream.read())!=-1){
      System.out.println((char)by);
}

读到没数据了就返回-1,这个用来判断是否读完。

//读一个字节数组,一般是1024大小
int len = 0 ;
byte[] bys = new byte[1024];
while ((len = inputStream.read(bys)) != -1) {
    System.out.println(new String(bys,0,len));
}

两个read的返回值略有不同,read()返回读取的字节,读到末尾返回-1,read(byte[] b)返回的是读到的字节个数,读到的字节放在了bytes字节数组里,读到末尾没数据了返回-1。

两种读取方式图解:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Yv0oyKLy-1603813798619)(http://p5kllyq5h.bkt.clouddn.com/174401.jpg)]

同样的用完了流,也要及时的关闭,以防占用内存。

inputStream.close();

完整示例:

建议以字节数组的方式读取数据。

package io2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Create by stefan
 * Date on 2018-05-27  23:00
 * Convertion over Configuration!
 */
public class input2 {
    public static void main(String args[]){
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream("a.txt");
//            byte[] bys = new byte[4];
//            int len = inputStream.read(bys);
//            System.out.println(new String(bys));  //bcd
//            System.out.println(len);  //3
//            System.out.println(inputStream.read(bys));  //-1
            int len = 0 ;
            byte[] bys = new byte[1024];
            while ((len = inputStream.read(bys)) != -1) {
                System.out.println(new String(bys,0,len));
            }
            /**
             *    public String(byte bytes[]) {
             this(bytes, 0, bytes.length);
             }
             */

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

字节流复制文件

利用输入流读取一个文件里的字节,再利用输出流将读取到的字节写出到另一个文件中(不存在会自动创建)

package io2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

/**
 * Create by stefan
 * Date on 2018-05-27  23:19
 * Convertion over Configuration!
 */
public class copy {
    public static void main(String args[]) throws IOException {
        FileInputStream inputStream = new FileInputStream("E:\\huge1.jpg");
        FileOutputStream outputStream = new FileOutputStream("E:\\古月.jpg");
        
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len=inputStream.read(bytes)) != -1) {
            outputStream.write(bytes,0,len);
        }
        inputStream.close();
        outputStream.close();
    }
}

注:复制文本、图片、mp3、视频等的方式一样。

字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,这是加入了数组这样的缓冲区效果。

java本身在设计的时候,也考虑到了这样的设计思想(装饰设计模式后面讲解),所以提供了字节缓冲区流。

字节缓冲输出流
BufferedOutputStream
字节缓冲输入流
BufferedInputStream
BufferedOutputStream
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a.txt",true));
bos.write("hello world".getBytes());
bos.close();
BufferedInputStream
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));
byte[] bytes = new byte[1024];
int len = 0;
while ((len=bis.read(bytes)) != -1) {
    System.out.println(new String(bytes,0,len));
}
bis.close();

注:

  • 成员方法与字节流基本一样,字节缓冲流的作用就是提高输入输出的效率。
  • 构造方法可以指定缓冲区的大小,但是我们一般用不上,因为默认缓冲区大小就足够了。
  • 为什么不传递一个具体的文件或者文件路径,而是传递一个OutputStream对象呢?原因很简单,字节缓冲区流仅仅提供缓冲区,为高效而设计的。但是呢,真正的读写操作还得靠基本的流对象实现。
复制文件的升级:
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\modern-java.pdf"));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\汤包\\慕课大巴\\modern-java.pdf"));
int len = 0;
byte[] bytes =new byte[1024];
while ((len=bis.read(bytes)) != -1) {
    bos.write(bytes,0,len);
}
bis.close();
bos.close();

测试:四种复制文件的效率高低

package io2;

import java.io.*;

/**
 *
 * 测试复制的时间
 * Create by stefan
 * Date on 2018-05-28  10:28
 * Convertion over Configuration!
 */
public class copy2 {
    //一个字节一个字节的复制,耗时22697毫秒
    public static  void  fun() throws IOException {
        FileInputStream fis = new FileInputStream("F:\\汤包\\慕课大巴\\modern-java.pdf");
        FileOutputStream fos = new FileOutputStream("E:\\modern-java.pdf");
        int by = 0;
        while ((by=fis.read()) != -1) {
            fos.write(by);
        }
        fis.close();
        fos.close();
    }
    //1024字节数组复制 耗时63毫秒
    public  static void  fun1() throws IOException {
        FileInputStream fis = new FileInputStream("F:\\汤包\\慕课大巴\\modern-java.pdf");
        FileOutputStream fos = new FileOutputStream("E:\\modern-java.pdf");
        int len = 0;
        byte[] bytes =new byte[1024];
        while ((len=fis.read(bytes)) != -1) {
            fos.write(bytes,0,len);
        }
        fis.close();
        fos.close();
    }
    // 一个字节一个字节复制,但是用了缓冲流 耗时64毫秒
    public static   void  fun2() throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\modern-java.pdf"));
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\汤包\\慕课大巴\\modern-java.pdf"));
        int by = 0;
        while ((by=bis.read()) != -1) {
            bos.write(by);
        }
        bis.close();
        bos.close();
    }
    // 1024字节数组复制并用了缓冲流 耗时7毫秒
    public  static void  fun3() throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\modern-java.pdf"));
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\汤包\\慕课大巴\\modern-java.pdf"));
        int len = 0;
        byte[] bytes =new byte[1024];
        while ((len=bis.read(bytes)) != -1) {
            bos.write(bytes,0,len);
        }
        bis.close();
        bos.close();
    }

    public static void main(String args[]) throws IOException {
        long t1 = System.currentTimeMillis();
        fun3();
        long t2 = System.currentTimeMillis();
        System.out.println(t2-t1);
    }

}

经测试结果显示:
1024字节数组复制并用了缓冲流 的方式效率最高。

以上是本人学习笔记整理,重温java经典,欢迎各位同道中人批评指正。

源码码云地址:
https://gitee.com/stefanpy/java

梦回io流完整目录:

java基础io流——File告白(重温经典)

java基础io流——OutputStream和InputStream的故事(温故知新)

java基础io流——字符流的变革(深入浅出)

java基础io流——配角也风流(不求甚解)

  • 118
    点赞
  • 418
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 15
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐同学呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值