Java技术篇·IO包·字节流

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/womeng2009/article/details/84201312

前言

Java中,IO流从数据流向可以分为输入流、输出流,从外部设备流向中央处理器的数据流成为“输入流”,反之成为“输出流”。流还可分为字节流、字符流。字节流和字符流的区别:

  1. 字节流读取的时候,读到一个字节就返回一个字节;字符流使用了字节流读到一个或多个字节(中文对应的字节数是两个,在UTF-8码表中是3个字节)时。先去查指定的编码表,将查到的字符返回。
  2. 字节流可以处理所有类型数据,如:图片,MP3,AVI视频文件,而字符流只能处理字符数据。只要是处理纯文本数据,就要优先考虑使用字符流,除此之外都用字节流。

一、字节流

1)InputStream、OutputStream

InputStream抽象了应用程序读取数据的方式

OutputStream抽象了应用程序写出数据的方式

2)文件结尾 EOF = END 或读到-1就读到结尾

3)输入流基本方法

int b = in.read();读取一个字节无符号填充到int低8位,读到-1是EOF

in.read(byte[] buf);读取数据填充到字节数组buf

in.read(byte[] buf, int start, int size);读取数据到字节数组buf,从buf的start位置开始存放size长度的数据

4)输出流基本方法

out.write(int b);写出一个byte到流,b的低8位

out.write(byte[] buf);将buf字节数组都写入到流

out.write(byte[] buf, int start, int size);字节数组buf从start位置开始写size长度的字节到流

二、字节流之文件输入流(FileInputStream)

FileInputStream继承自InputStream,具体实现了在文件上读取数据。

package com.io;

import java.io.*;

/**
 * FileInputStream继承自InputStream
 * 具体实现了在文件上读取数据
 */
public class FileInputStreamTest {
    public static void main(String[] args) {
        File file = new File("/Users/xxx/Downloads/io_test/fileInputStream.txt");
        InputStream is = null;
        try {
            is = new FileInputStream(file);
            byte[] bytes = new byte[128];
            int i = 1;
            while (is.read(bytes, 0, bytes.length) != -1) {
                for (byte b : bytes) {
                    System.out.print(Integer.toHexString(b & 0xff) + " ");
                    if (i++%10 == 0) {
                        System.out.println();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
/*
* 输出
* e6 9c 89 e5 85 b3 e7 9f a5 e8
* af 86 e5 8f 8a e8 af b4 e6 98
* 8e ef bc 8c e5 85 a8 e9 83 a8
* e5 9c a8 e4 b8 8b e8 be b9 e7
* a8 8b e5 ba 8f e4 b8 ad e4 bd
* 93 e7 8e b0 e3 80 82 20 70 61
* 63 6b 61 67 65 20 63 6f 6d 2e
* 69 6f 3b 20 69 6d 70 6f 72 74
* 20 6a 61 76 61 2e 69 6f 2e 46
* 69 6c 65 3b 20 69 6d 70 6f 72
* 74 20 6a 61 76 61 2e 69 6f 2e
* 52 61 6e 64 6f 6d 41 63 63 65
* 73 73 46 69 6c 65 3b 20 2f 2a
* 2a 20 2a 20 52 61 6e 64 6f 6d
* 41 63 63 65 73 73 46 69 6c 65
* 20 4a 61 76 61 e6 8f 90 e4 be
* 9b e7 9a 84 e5 af b9 e6 96 87
* e4 bb b6 e5 86 85 e5 ae b9 e7
* 9a 84 e8 ae bf e9 97 ae ef bc
* 8c e6 97 a2 e5 8f af e4 bb a5
* e8 af bb e4 b9 9f e5 8f af e4
* bb a5 e5 86 99 e3 80 82 69 6c
* 65 3b 20 69 6d 70 6f 72 74 20
* 6a 61 76 61 2e 69 6f 2e 52 61
* 6e 64 6f 6d 41 63 63 65 73 73
* 46 69 6c 65 3b 20
 */

三、字节流之文件输出流(FileOutputStream)

FileOutputStream继承自OutputStream,具体实现了向文件写出byte数据的方法。

package com.io;

import java.io.*;

/**
 * FileOutputStream继承自OutputStream
 * 实现了向文件中写出byte数据的方法
 */
public class FileOutputStreamTest {
    public static void main(String[] args) {
        String str = "Java中默认的编码方式: 编码问题存在两个方面:JVM之内和JVM之外。 " +
                "1、Java文件编译后形成class 这里Java文件的编码可能有多种多样," +
                "但Java编译器会自动将这些编码按照Java文件的编码格式正确读取后产生class文件," +
                "这里的class文件编码是Unicode编码";
        File file = new File("/Users/xxx/Downloads/io_test/fileOutStream.txt");
        OutputStream out = null;
        try {
            out = new FileOutputStream(file);
            byte[] bytes = str.getBytes("utf-8");
            out.write(bytes);
            out.flush();    //可刷新,也可不刷新,最好刷新缓冲区
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

文件输出:

四、DataInputStream/DataOutputStream

对“流”功能的扩展,可以更加方便的读取int、long等类型的数据。但是要保证,以什么形式存储,就以什么形式读取。

程序示例:

package com.io;

import java.io.*;

public class DataOutputStreamTest {
    public static void main(String[] args) {
        String str = "业务场景介绍 有一个表字段存储了另一个表的关联字段," +
                "以“|”符号分割,如下所示: 连接查询中当然可以使用like来关联," +
                "但是效率很差。高效的做法应该是将该字段按照“|”字符分割,然后关联查询。" +
                " 二、oracle分割查询方法 SELECT regexp_substr( 'a|b|...";
        File file = new File("/Users/xxx/Downloads/io_test/dataOutputStream.txt");
        DataOutputStream dataOutputStream = null;
        try {
            dataOutputStream = new DataOutputStream(new FileOutputStream(file));
            dataOutputStream.writeUTF(str);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dataOutputStream != null) {
                try {
                    dataOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
package com.io;

import java.io.*;

public class DataInputStreamTest {
    public static void main(String[] args) {
        File file = new File("/Users/xxx/Downloads/io_test/dataOutputStream.txt");
        DataInputStream dataInputStream = null;
        try {
            dataInputStream = new DataInputStream(new FileInputStream(file));
            System.out.println(dataInputStream.readUTF());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dataInputStream != null) {
                try {
                    dataInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
/*
输出
业务场景介绍 有一个表字段存储了另一个表的关联字段,以“|”符号分割,如下所示: 连接查询中当然可以使用like来关联,但是效率很差。高效的做法应该是将该字段按照“|”字符分割,然后关联查询。 二、oracle分割查询方法 SELECT regexp_substr( 'a|b|...
*/

五、字节流之缓冲流(BufferedInputStream&BufferedOutputStream)

这两个流类为IO提供了带缓冲区的操作,一般打开文件进行写入或读区操作时,都会加上缓冲,这种流模式提高了IO的性能。

先将需要写入文件的内容放在缓冲区,可在某个节点手动flush将数据写入文件。

程序示例:

package com.io;

import java.io.*;

public class BufferedStreamTest {
    public static void copyByBuffer(File srcFile, File desFile) throws Exception {
        BufferedInputStream inBuffer = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream outBuffer = new BufferedOutputStream(new FileOutputStream(desFile));
        int b;
        while ((b = inBuffer.read()) != -1) {
            outBuffer.write(b);
        }
        outBuffer.flush();
        inBuffer.close();
        outBuffer.close();
    }

    public static void main(String[] args) {
        try {
            long start = System.currentTimeMillis();
            copyByBuffer(new File("/Users/xxx/Downloads/io_test/111.jpg"),
                    new File("/Users/xxx/Downloads/io_test/222.jpg"));
            long end = System.currentTimeMillis();
            System.out.println("总用时:" + (end - start) + "毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
/*
* 总用时:4毫秒
* */

执行结果:

猜你喜欢

转载自blog.csdn.net/womeng2009/article/details/84201312
今日推荐