博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OutputStream、InputStream 、FileOutputStream、FileInputStream,字节流API
阅读量:6904 次
发布时间:2019-06-27

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

OutputStream、InputStream是所有字节流的超类,所谓字节流就是一个字节一个字节的读取,类似于c中的二进制读取方法

java.io.OutputStream

Method

  void write(int b)

    Writes the specified byte to this output stream

  void write(byte[] b)    //   String 类的方法,  byte[] b = "中国".getBytes();   

    Writes b.length bytes from the specified byte array to this output stream.

  void write(byte[], int offset, int len)

    Writes len bytes from the specified byte array starting at offset off to this output stream.

OutputStream out = new FileOutputStream("E:/a.txt");        out.write(97);   // 写下二进制的97,文本编辑器读出二进制97,查询本机编码表,得出a                byte[] b = {-43,-44};   // 赵        out.write(b);        out.close();
View Code

  void flush()

    Flushes this output stream and forces any buffered output bytes to be written out.

  void close()

子类 FileOutputStream

构造函数

  FileOutputStream(File file, boolean append)     //  append 参数不写默认为false,即每次都会创建一个新文件(不管文件是否存在)

    Creates a file output stream to write to the file represented by the specified File object.

  FileOutputStream(String name, boolean append)   // @name  the file path @append 同上

其余方法暂时略,大部分与父类相同

 

java.io.InputStream

  int read()

    Reads the next byte of data from the input stream

  int read(byte[] b)

    Reads some number of bytes from the input stream and stores them into the buffer array b.

  int read(byte[] b, int off, int len)

    Reads up to len bytes of data from the input stream into an array of bytes.

  

InputStream in = new FileInputStream("E:/a.txt");  //文件内容 abcde        //int a = in.read();   // 97         byte[] b = new byte[2];        int a = in.read(b);   // a值 2   b值 ab        a = in.read(b);   //  a值  2     b值 cd        a = in.read(b);   // a值 1       b值 ed        a = in.read(b);   // a值 -1      b值 ed                 // a 为读出的字节数, 读到文件末尾时为 -1        in.close();
View Code

子类 FileInputStream

构造函数

  FileInputStream(File file, boolean append)

  FileInputStream(String name, boolean append)  // 同上

 

二进制文件复制

InputStream in = new FileInputStream("E:/test.avi");        OutputStream out = new FileOutputStream("E:/copy.avi");                int len;        byte[] b = new byte[1024];        while((len = in.read(b)) != -1)        {            out.write(b, 0, len);            // out.flush();        }                in.close();        out.close();
View Code

BufferedOutputStream、BufferedInputStream 提高程序读取效率

构造函数 BufferedOutputStream(OutputStream out)

下面是一个对比,复制102M的文件耗时

public static void copy_4(String desPath, String srcPath) throws IOException{                 BufferedInputStream in = new BufferedInputStream(new FileInputStream(srcPath));           BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(desPath));                int len;        byte[] b = new byte[1024];        while((len = in.read(b)) != -1)        {            out.write(b, 0, len);                 }                in.close();        out.close();}/*     * 一个字节一个的读,没缓冲     408,891     * 一个字节一个的读,有缓冲       4979     * 每次读 1024字节, 没缓冲      898     * 每次读 1024字节, 有缓冲      236    */
View Code

 

转载于:https://www.cnblogs.com/YKang/p/7283333.html

你可能感兴趣的文章
实验四 恶意代码技术
查看>>
快速打出System.out.println("");
查看>>
kermit的安装、配置、使用
查看>>
shell编程学习
查看>>
忙中记录
查看>>
Js点餐加减数量
查看>>
【转】ACM训练计划
查看>>
Design Tic-Tac-Toe
查看>>
LeetCode 477: Total Hamming Distance
查看>>
win10安装MarkdownPad 2报错This view has crashed的处理及md简单语法
查看>>
Unity3D - 设计模式 - 工厂模式
查看>>
第二十六课:jQuery对事件对象的修复
查看>>
Leetcode题目:Swap Nodes in Pairs
查看>>
Windows聚焦转为图片
查看>>
POJ NOI0101-09 字符菱形
查看>>
jQuery--停止动画和判断是否处于动画状态stop()
查看>>
1-1 接口自动化测试框架从设计到开发
查看>>
MYSQL常用命令
查看>>
js 打开新页面 window.open()
查看>>
Intellij idea 一个窗口打开多模块并添加依赖
查看>>