字节流和字符流

1、什么是字节流,什么是字符流

  1. 字节流: 它处理单元为1个字节(byte),操作字节和字节数组,存储的是二进制文件,如果是音频文件、图片、歌曲,就用字节流好点(1byte = 8位);
  2. 字符流: 它处理的单元为2个字节的Unicode字符,分别操作字符、字符数组或字符串,字符流是由Java虚拟机将字节转化为2个字节的Unicode字符为单位的字符而成的,如果是关系到中文(文本)的,用字符流好点(1Unicode = 2字节 = 16位);

所有文件的储存是都是字节(byte)的储存,在磁盘上保留的并不是文件的字符而是先把字符编码成字节,再储存这些字节到磁盘。在读取文件(特别是文本文件)时,也是一个字节一个字节地读取以形成字节序列。

  • 字节流可用于任何类型的对象,包括二进制对象,而字符流只能处理字符或者字符串;
  • 字节流提供了处理任何类型的IO操作的功能,但它不能直接处理Unicode字符,而字符流就可以。

字节流是最基本的,所有的InputStremOutputStream的子类都是,主要用在处理二进制数据,它是按字节来处理的 但实际中很多的数据是文本,又提出了字符流的概念,它是按虚拟机的encode来处理,也就是要进行字符集的转化 这两个之间通过 InputStreamReader,OutputStreamWriter来关联,实际上是通过byte[]和String来关联。在实际开发中出现的汉字问题实际上都是在字符流和字节流之间转化不统一而造成的。

最简单的区分字节流和字符流:

万物皆文件,那就将文件在记事本里面打开,如果打开后能看的懂的就是字符流,如果看不懂那就是字节流。

问题:

  • word.doc 数据字节流还是字符流?

答:.doc数据字节流。

  • Excel 数据字节流还是字符流?

答:要根据保存的格式进行判断,如果是保存为.csv那么他就是字符流,如果是其他的则数据字节流。

字符和字节操作

如何简单记住字节流和字符流

  • 字符流是以ReaderWriter结尾的
  • 字节流是以InputStreamOutputStream结尾的

2、字节流的使用

字节流主要是操作byte类型数据,以byte数组为准,主要操作类就是OutputStream、InputStream。

字节输出流:OutputStream,OutputStream是整个IO包中字节输出流的最大父类

Closeable表示可以关闭的操作

2.1普通字节流的操作

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

/**
 * 复制D:\io_test下的加菲猫文件到D:\io_test这个目录下
 */

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 = null;
        FileOutputStream fileOutputStream = null;
        try {
            //输入流,传入路径
            fileInputStream = new FileInputStream(srcFilePath);
            //输出流
            fileOutputStream = new FileOutputStream(destFilePath);
            byte[] bytes = new byte[1024];
            try {
                int count = 0;
                //如果不等于-1说明还没有读取完成,要继续读取
                while((count = fileInputStream.read(bytes)) != -1){
                    //还有内容要读取,然后从0开始读取,上面count有读了多少个,那么就写多少个
                    fileOutputStream.write(bytes,0,count);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            //用完之后一定要关闭流,但是得判断不是空的时候才需要去关闭流
            if(fileInputStream != null){
                fileInputStream.close();
            }
            if(fileOutputStream != null){
                fileOutputStream.close();
            }
        }
    }
}

注意一定要关闭流:

  • 因为每天计算机打开的文件的数量是有限的,以liunx为例,最大可以打开66533个文件,
  • 但是无论执行结果怎么样他都要关闭流,那么就需要将他放在finally里面,所有上面的声明也得放在外面
  • 如果传入的地址和目标地址都是错的,也就是找不到,如果传入的地址错了,那么还没有初始化就已经被调用关闭流了,所以要进行判断

2.2带有缓冲区的字节流的操作

什么是缓存区?
定义:缓存区相当于缓存,它是存在内存中的

写操作:

  • 没有使用缓存区:CPU读取每个字节之后直接操作磁盘(性能比较底)进行写完,写操作的瓶颈就会出现,因为每个字节都会操作一次磁盘
  • 使用缓冲区:那么每次会将字符放入缓存区(内存),等缓冲区满了之后,才一次性写入磁盘

因为内存的操作速度远远大于磁盘,因此带缓冲区的输入流和输出流实现的效率就非常高(比如扔垃圾,一次性扔完和一次次扔肯定消耗的时间是有很大差距的)

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();
        }
    }
}

3、字符流的使用

3.1普通的字符流操作

字符输出流:Writer
字符输入流:Reader

3.1.1内容的写入

import java.io.FileWriter;
import java.io.IOException;

/**
 * 内容的写入:D:\io_test\1下创建一个text.txt文件,并写入内容
 */
public class IoDemo7 {
    public static void main(String[] args) throws IOException {
        //定义文件地址
        String filePath = "D:\\io_test\\1\\text.txt";
        //写入内容
        String content = "欢迎来到Java~";
        //因为是写入文件,所以要使用FileWriter方法
        FileWriter fileWriter = null;
        try {
            //因为这里的参数append会被默认成false,所以如果是多行写入的话需要手动设置成true
            fileWriter = new FileWriter(filePath,false);
            fileWriter.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭流
            fileWriter.close();
        }
    }
}

注意:如果不关闭流,虽然也能创建文件成功,不报错,但是可能存在内容无法写入的情况

3.1.2将文件下的内容读取到控制台

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 * 读取文件内容
 */
public class IoDemo8 {
    public static void main(String[] args) throws FileNotFoundException {
        String filePath = "D:\\io_test\\1\\tt.txt";
        Scanner scanner = new Scanner(new File(filePath));
        //因为读取的内容可能不止一条,所以加上while循环
        while (scanner.hasNext()){
            System.out.println(scanner.nextLine());
        }
    }
}

3.1.3将一个路径下的内容复制到另一个路径下

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 *将一个文件的内容写入到另一个文件下
 * 如:D:\io_test\1\text.txt -> text2.txt
 */
public class IoDemo9 {
    public static void main(String[] args) throws IOException {
        //读取文件的地址
        String srcFilePath = "D:\\io_test\\1\\text.txt";
        //目标文件的地址
        String destFilePath = "D:\\io_test\\1\\text2.txt";
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        //先读后写
        try{
            fileReader = new FileReader(srcFilePath);
            fileWriter = new FileWriter(destFilePath);
            {
                //读操作
                char[] chars = new char[1024];
                while (true){
                    int count = fileReader.read(chars);
                    //判断是否等于-1,如果等于-1,说明已经读取完了,跳出循环
                    if(count == -1){
                        break;
                    }
                    //写操作
                    fileWriter.write(chars,0,count);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            fileReader.close();
            fileWriter.close();
        }
    }
}

3.2带有缓冲区的字符流操作

内容的复制

import java.io.*;

/**
 * 带有缓冲区的字符流操作
 * 如:内容的复制
 */
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";
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        //先读取,后写入
        try {
            bufferedReader = new BufferedReader(new FileReader(srcFilePath));
            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 (Exception e){
            e.printStackTrace();
        }finally {
            bufferedReader.close();
            bufferedWriter.close();
        }
    }
}

4、总结

  • 字节流操作的基本单元是字节;字符流操作的基本单元为Unicode码元。
  • 字节流在操作的时候本身不会用到缓冲区的,是与文件本身直接操作的;而字符流在操作的时候使用到缓冲区的。
  • 所有文件的存储都是字节(byte)的存储,在磁盘上保留的是字节。
  • 在使用字节流操作中,即使没有关闭资源(close方法),也能输出;而字符流不使用close方法的话,不会输出任何内容

评论

暂无

添加新评论