博客
关于我
字节流和字符流详解
阅读量:623 次
发布时间:2019-03-13

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

流在编程中是一个非常重要的概念,它用来描述数据在程序中的传输和存储方式。流就是像水流一样,从一端流向另一端。在编程中,数据也以流的形式进行传输或保存。程序在运行时,需要读取数据或将数据输出,这都需要通过流来实现。

流的分类

根据流的方向,流可以分为输入流和输出流:

  • 输入流(InputStream):从外部源读取数据,如键盘、麦克风、网络反向链接等。
  • 输出流(OutputStream):向外部目标输出数据,如显示器、音箱、文件等。

根据流的传输单位,流可以分为字节流和字符流:

  • 字节流(ByteStream):以字节(byte)为基本单位处理数据,主要操作类为InputStream和OutputStream,适用于处理二进制文件,例如图片、音乐等。
  • 字符流(CharacterStream):以字符(Character)为基本单位处理数据,字符流通过将字节转换为Unicode字符为单位进行操作,适用于处理文本文件。

字节流与字符流的区别

  • 字节流处理单元为一个字节(8位),操作对象为字节和字节数组,适用于处理所有类型的文件,包括二进制文件。
  • 字符流处理单元为两个字节(16位)的Unicode字符,操作对象为字符、字符数组或字符串,字符流是Java虚拟机将字节转换为字符为单位的字符流,适用于处理文本文件。

所有文件在存储时都是以字节形式保存的,磁盘上保存的是字节,而不是字符。读取文件时,必须将字节转换为字符才能显示或处理。这表明,文件操作的核心实际上是字节流的操作。

字节流和字符流之间的转换可以通过InputStreamReader和OutputStreamWriter实现。这些转换器将字节流和字符流连接起来,允许程序在处理数据时灵活选择使用字节流还是字符流。

字节流的使用

字节流用于处理所有类型的数据,包括二进制文件。常用的字节流类有FileInputStream和FileOutputStream,它们分别用于读取和写入文件。以下是一个字节流操作的示例:

import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class IoDemo4 {    public static void main(String[] args) throws IOException {        String srcFilePath = "D:\\io_test\\加菲猫.jpg";        String destFilePath = "D:\\io_test\\加菲猫2.jpg";                FileInputStream fileInputStream = new FileInputStream(srcFilePath);        FileOutputStream fileOutputStream = new FileOutputStream(destFilePath);        byte[] bytes = new byte[1024];        try {            int count = 0;            while ((count = fileInputStream.read(bytes)) != -1) {                fileOutputStream.write(bytes, 0, count);            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (fileInputStream != null) {                fileInputStream.close();            }            if (fileOutputStream != null) {                fileOutputStream.close();            }        }    }}

带缓冲区的字节流操作

使用缓冲区可以提高IO操作的性能。缓冲区将数据在内存中暂存,减少磁盘操作的次数。以下是一个带缓冲区的字节流操作示例:

import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;public class IoDemo5 {    public static void main(String[] args) {        String srcFilePath = "D:\\io_test\\加菲猫.jpg";        String destFilePath = "D:\\io_test\\加菲猫3.jpg";        try {            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFilePath));            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destFilePath));            byte[] bytes = new byte[1024];            int count = 0;            while ((count = bufferedInputStream.read(bytes)) != -1) {                bufferedOutputStream.write(bytes, 0, count);            }        } catch (Exception e) {            e.printStackTrace();        }    }}

字符流的使用

字符流专门用于处理文本数据。字符流的基本类是Reader和Writer。以下是一个字符流操作的示例:

import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class IoDemo7 {    public static void main(String[] args) throws IOException {        String filePath = "D:\\io_test\\1\\text.txt";        String content = "欢迎来到Java~";        FileWriter fileWriter = new FileWriter(filePath);        try {            fileWriter.write(content);        } catch (IOException e) {            e.printStackTrace();        } finally {            fileWriter.close();        }    }}

带缓冲区的字符流操作

字符流也可以使用缓冲区来提高性能。以下是一个带缓冲区的字符流操作示例:

import java.io.BufferedReader;import java.io BufferedWriter;import java.io FileReader;import java.io FileWriter;import java.io.IOException;public class IoDemo10 {    public static void main(String[] args) throws IOException {        String srcFilePath = "D:\\io_test\\1\\tt.txt";        String destFilePath = "D:\\io_test\\1\\tt2.txt";        try {            BufferedReader bufferedReader = new BufferedReader(new FileReader(srcFilePath));            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(destFilePath));            char[] chars = new char[1024];            while (true) {                int count = bufferedReader.read(chars);                if (count == -1) {                    break;                }                bufferedWriter.write(chars, 0, count);            }        } catch (IOException e) {            e.printStackTrace();        } finally {            bufferedReader.close();            bufferedWriter.close();        }    }}

总结

  • 字节流:处理单个字节,适用于所有文件类型,特别是二进制文件。
  • 字符流:处理Unicode字符,适用于文本文件。
  • 缓冲区:提高IO性能,减少磁盘操作次数。
  • 使用场景:根据文件类型和处理需求选择流类型。

通过实际练习字节流和字符流的操作,我对它们的使用更加熟悉,也明白了在实际开发中如何根据需求选择合适的IO操作流类型。

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

你可能感兴趣的文章
Objective-C实现Bilateral Filter双边滤波器算法(附完整源码)
查看>>
Objective-C实现binary exponentiation二进制幂运算算法(附完整源码)
查看>>
Objective-C实现binary search二分查找算法(附完整源码)
查看>>
Objective-C实现binary tree mirror二叉树镜像算法(附完整源码)
查看>>
Objective-C实现binary tree traversal二叉树遍历算法(附完整源码)
查看>>
Objective-C实现BinarySearchTreeNode树算法(附完整源码)
查看>>
Objective-C实现binarySearch二分查找算法(附完整源码)
查看>>
Objective-C实现binomial coefficient二项式系数算法(附完整源码)
查看>>
Objective-C实现binomial distribution二项分布算法(附完整源码)
查看>>
Objective-C实现bisection二分法算法(附完整源码)
查看>>
Objective-C实现bisection二等分算法(附完整源码)
查看>>
Objective-C实现BitMap算法(附完整源码)
查看>>
Objective-C实现bitmask位掩码算法(附完整源码)
查看>>
Objective-C实现bitonic sort双调排序算法(附完整源码)
查看>>
Objective-C实现BloomFilter布隆过滤器的算法(附完整源码)
查看>>
Objective-C实现BMP图像旋转180度(附完整源码)
查看>>
Objective-C实现bogo sort排序算法(附完整源码)
查看>>
Objective-C实现boruvka博鲁夫卡算法(附完整源码)
查看>>
Objective-C实现Boyer-Moore字符串搜索算法(附完整源码)
查看>>
Objective-C实现BP误差逆传播算法(附完整源码)
查看>>