博客
关于我
字节流和字符流详解
阅读量: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/

你可能感兴趣的文章
python 列表转字典方法
查看>>
Python 列表(List)概述-ChatGPT4o作答
查看>>
Python网络爬虫基础进阶到实战教程
查看>>
Python 初学者需要知道的四条建议
查看>>
Python 判断字符串是否包含子字符串
查看>>
python 判断当前时间是否为零点
查看>>
python 利用pandas读取本地中CSV文件的指定列 列名重命名 并保存回本地
查看>>
python 利用pyspark读取HDFS中CSV文件的指定列 列名重命名 并保存回HDFS
查看>>
python 利用pyttsx3文字转语音
查看>>
python 利用已有Ner模型进行数据清洗合并
查看>>
python 到大数据开发工程师_如何成为一个大数据开发工程师?
查看>>
python 加密解密(base64, AES)
查看>>
Python 包管理器和 Node.js
查看>>
python 启动提示IDLE's subprocess didn't make conne...
查看>>
Python 和Java 哪个更适合做自动化测试?
查看>>
Python编程:掌握高级语言程序设计。从零基础到精通,收藏这篇就够了!
查看>>
python 图片转ico
查看>>
python 图片转文字、语音转文字、文字转语音保存音频并朗读
查看>>
python 在包含类似字符\x16、\x12、\x某某的数组中将以\x开头的字符找出来的方法
查看>>
Python 在并行进程之间共享字典
查看>>