Java IO:字节流、字符流、缓冲流2023.8.16

1、Java IO流原理与流的分类

1.1 Java IO流原理

(1)I/O是Input/Output的缩写,I/0技术是非常实用的技术,用于处理数据传输,如读/写文件,网络通讯等。Java程序中,对于数据的输入/输出操作以”流(stream)” 的方式进行;
(2)java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过方法输入或输出数据;
(3)输入Input:读取外部数据,并存储至程序的内存中;
(4)输出Output:将程序中的数据输出至磁盘、光盘等存储设备中

1.2 IO流的分类

(1)按数据流的方向:输入流、输出流;
(2)按处理数据单位:字节流、字符流;
(3)按功能:节点流、处理流。

字节流与字符流的表示如下:图1
字节流与字符流的功能使用如下:
图2
  字节流操作的单元是数据单元是8位的字节,字符流操作的是数据单元为16位的字符,所以对于非纯文本的文件,一般采用字节流来使用。
  Java中字符是采用Unicode标准,Unicode 编码中,一个英文字母或一个中文汉字为两个字节。而在UTF-8编码中,一个中文字符是3个字节。如果使用字节流处理中文,如果一次读写一个字符对应的字节数就不会有问题,一旦将一个字符对应的字节分裂开来,就会出现乱码了。为了更方便地处理中文这些字符,Java就推出了字符流。
  字节流一般用来处理图像、视频、音频、PPT、Word等类型的文件。字符流一般用于处理纯文本类型的文件,如TXT文件等,但不能处理图像视频等非文本文件。用一句话说就是:字节流可以处理一切文件,而字符流只能处理纯文本文件。
  字节流本身没有缓冲区,缓冲字节流相对于字节流,效率提升非常高。而字符流本身就带有缓冲区,缓冲字符流相对于字符流效率提升就不是那么大了。详见文末效率对比。

2、IO流常用体系

2.1 FileInputStream介绍及应用

  FileInputStream为字节输入流,主要的功能为将文件内容读取至程序,下面给了两个使用示例,代码如下:

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class FileInputScream1 {
    @Test
    public void readFile01(){
        String filePath="e://liuzhuohang.txt";
        int readData=0;
        FileInputStream fileInputStream=null;
        try {
            fileInputStream = new FileInputStream(filePath);
            while ((readData=fileInputStream.read())!=-1){
                System.out.print((char)readData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @Test
    public void readFile02(){
        String filePath="e://liuzhuohang.txt";
        int readData=0;
        byte[] by=new byte[8];
        FileInputStream fileInputStream=null;
        try {
            fileInputStream = new FileInputStream(filePath);
            while ((readData=fileInputStream.read(by))!=-1){
                System.out.print(new String(by,0,readData));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

2.2 FileOutputStream介绍及应用

  FileOutputStream为字节输出流,主要功能为从程序内存中读取文件并输出至设备,样例代码如下:

import org.junit.jupiter.api.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputstreasm01 {
    public static void main(String[] args) {
    }
    @Test
    public  void writeFile(){
        String filePath="e://liuzhuohang.txt";
        FileOutputStream fileOutputStream=null;
        try {
        //样例后加true为在源文件上进行内容增加,不加true为源文件直接重写
            fileOutputStream=new FileOutputStream(filePath,true);
            try {
                fileOutputStream.write('G');
                String str="liuzhuohang";
                fileOutputStream.write(str.getBytes(),2,3);
                System.out.println("输入成功");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

2.3 FileReader介绍及应用

  FileReader为字符输入流,将文件以字符流的形式输入至内存中,代码示例如下:

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

public class FileReader1 {
    public static void main(String[] args) {
        String filePath="e://liuzhuohang.txt";
        FileReader fileReader = null;
        int data=' ';
        try {
            fileReader=new FileReader(filePath);
            while ((data=fileReader.read())!=-1){
                System.out.print((char)data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            if(fileReader!=null){
                try {
                    fileReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    public void m1(){
        String filePath="e://liuzhuohang.txt";
        FileReader fileReader = null;
        int data=' ';
        char[] by=new char[8];
        try {
            fileReader=new FileReader(filePath);
            while ((data=fileReader.read(by))!=-1){
                System.out.print(new String(by,0,data));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

2.4 FileWriter介绍及应用

  FileWriter为字符输出流,主要功能为从程序内存中读取文件并输出至设备,样例代码如下:

import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriter01 {
    public static void main(String[] args) {
    }
    @Test
    public void FileWriter011(){
        FileWriter fileWriter01=null;
        String filePath="e://liuzhuohang.txt";
        try {
            fileWriter01=new FileWriter(filePath);
            fileWriter01.write('H');
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileWriter01.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

3、节点流与处理流

3.1 基本介绍

(1)节点流可以从一个特定的数据源读写数据,如FileReader、FileWriter等;
(2)处理流(也叫包装流)是“连接”已存在的流(节点流、处理流均可)之上,为程序提供更强大的读写功能,也更加灵活,如BufferedReader、BufferWriter等。

3.2 节点流与处理流一览图

细看

3.3 节点流与处理流的区别与联系

(1)节点流是底层流/低级流直接跟数据源相接;
(2)处理流(包装流)包装节点流,既可以消除不同节点流的实现差异,也可以提供更方便的方法来完成输入输出;
(3)处理流(也叫包装流)对节点流进行包装,使用了修饰器设计模式,不会直接与数据源相连。
  处理流的主要功能如下:
(1) 性能的提高:主要以增加缓冲的方式来提高输入输出的效率操作的便捷;
(2)处理流可能提供了一系列便捷的方法来一次输入输出大批量的数据,使用更加灵活方便。

3.4 BufferedReader、BufferWriter介绍及应用

  BufferedReader与BufferedWriter为处理流,主要用于处理节点流的文件,代码示例如下:

import com.oracle.webservices.internal.api.databinding.WSDLGenerator;
import org.junit.jupiter.api.Test;
import java.io.*;

//使用转换流解决中文乱码问题
public class Transformation {
    public static void main(String[] args) throws IOException {
        String filePath="e:\\liuzhuohang.txt";
        //将字节流FileInputStream转换为INputStreamReader
        InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath),"gbk");
        BufferedReader br=new BufferedReader(isr);
        String s=br.readLine();
        System.out.println(s);
        br.close();
    }
}
class OutPut{
    @Test
    public  void Out() throws IOException {
        String filePath="e:\\liuzhuohang.txt";
        //将字节流FileInputStream转换为INputStreamReader
        OutputStreamWriter isr = new OutputStreamWriter(new FileOutputStream(filePath),"gbk");
        BufferedWriter br=new BufferedWriter(isr);
        isr.write("sdaui");
        br.write("miss");
        System.out.write("按照".getBytes());
        br.close();
    }
}

3.5 BufferedInputStream、BufferedOutputStream介绍及应用

(1)BufferedInputStream是字节流在创建 BufferedInputStream时,会创建一个内部缓冲区数组;
(2)BufferedOutputStream是字节流,实现缓冲的输出流,可以将多个字节写入底层输出流中,而不必对每次字节写入调用底层系统。

import java.io.*;

public class BufferCopy {
    public static void main(String[] args) throws IOException {
        String filePath="e://liuzhuohang.txt";
        String endPath="e://liuzhuohang02.txt";
        BufferedInputStream bufferedInputStream = 
        new BufferedInputStream(new FileInputStream(filePath));
        BufferedOutputStream bufferedOutputStream=
        new BufferedOutputStream(new FileOutputStream(endPath));
        int line=0;
        while ((line= bufferedInputStream.read())!=-1){
            bufferedOutputStream.write(line);
        }
        System.out.println("文件转换完成");
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }
}

3.6 对象流ObjectOutputStream、ObjectInputStream介绍及应用

对象流提供了对基本类型或对象类型的序列化和反序列化的方法
(1)ObjectOutputStream:提供序列化功能,将程序写入存储中进行序列化固定;
(2)ObjectInputStream:提供反序列化功能,将存储的序列化文件从内存中取出,读入程序。

//有时间需补充

3.7 转换流OutputStreamWriter、InputStreamReader介绍及应用

有时间就补充

3.8 打印流

有时间就补充

4、Properties类

(1)专门用于读写配置文件的集合类配置文件的格式;
  键=值
  键=值
(2) 注意: 键值对不需要有空格,值不需要用引号一起来。默认类型是String。
(3)常用方法:
  load:加载配置文件的键值对到Properties对象;
  list:将数据显示到指定设备;
  getProperty(key):根据键获取值;
  setProperty(key;value):设置键值对到Properties对象;
  store:将Properties中的键值对存储到配置文件,在idea 中,保存信息到配置文件含有中文,会存储为unicode码
   相应的代码示例文件如下:

import java.io.*;
import java.util.Properties;

public class Properties03 {
    public static void main(String[] args) throws IOException {
        File file=new File("E:\\IDEA PROJECT\\chapter-10\\untitled\\src\\JavaHsp\\File\\Properties\\liuzhuohang.properties");
        file.createNewFile();
        Properties properties = new Properties();
        properties.setProperty("sdada","dsadsasdasd");
        properties.store(new FileOutputStream("E:\\IDEA PROJECT\\chapter-10\\untitled\\src\\JavaHsp\\File\\Properties\\liuzhuohang.properties"),null);
//        properties.load(new FileReader("E:\\IDEA PROJECT\\chapter-10\\untitled\\src\\JavaHsp\\File\\Properties\\mysql.properties"));
//        properties.list(System.out);
//        String user = properties.getProperty("user");
//        System.out.println(user);
    }
}

[1]: 【零基础 快速学Java】韩顺平 零基础30天学会Java_哔哩哔哩_bilibili
[2]: 云深不知处-CSDN博客_字节缓冲流和字符缓冲流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值