io流相关知识

io流相关知识

原则

如果进行输入/输出操作的内容是文本内容,则应该考虑使用字符流;如果是二进制内容,考虑使用字节流。

什么是io

i: input: 输入流: 从外向内. (将数据读取到内存中)
o: output: 输出流: 从内向外

体系架构

按照类型划分
image

字节流与字符流

字节流和字符流的用法几乎一样,区别在于字节流和字符流操作的数据单元不一样,(InputStream / OutputStream)字节流操作数据单元是8位的字节,(Reader/Writer)而字符流操作的是16位的字符。
在InputStream和Reader中主要包含3个方法:

int read()  从输入流中读取单个字符,返回所读取的字节数据(字节数据可以直接转换位int类型)
int read(byte[] b) 从输入流中最多读取 b.length 个字节的数据,并将其放到字节数组b中,返回实际读取的字数,如果没有读取到则返回-1。
int read(byte[] b,int off ,int len)其中,off是偏移量,len是读取的长度,并将其读取到字节数组中。

示例代码

package com.io;

import java.io.*;

public class Demo {
    public static void main(String[] args) {
        // 使用 FileOutputStream将数据写入到本地文件
        FileOutputStreamTest();

        // 使用 FileInputStream 读取本地文件
        FileInputStreamTest();

        // 使用FileWriter 将数据写入到本地磁盘
        FileWriterTest();

        // 使用FileReader 读取本地文件
        FileReaderTest();
    }

    // 使用 FileOutputStream将数据写入到本地文件
    /*
    文件字节输出流FileOutputStream
    1、可以将字节数据写出到指定文件中
    2、构造方法:
        (1)FileOutputStream(File f):将f描述的路径文件封装成字节输出流对象
        (2)FIleOutputStream(String path):将path描述的路径封装成字节输出流对象
        (3)FileOutputStream(String path,boolean append):
            如果第二个参数为true,这次写出的内容,则写出自文件原有内容之后,进行拼接,不会清空原有的内容,
            如果不写,或者为false,则相当于创建了一个新的该文件,再写入内容
    3、字节流写出数据的方式
        (1)write(int b):将指定的字节写出到目标文件中,一次写出一个字节信息
        (2)write(byte[] b):一次将一个数组的内容写入到指定文件中
        (3)write(byte[] b,int off,int len):将len个字节从指定的字节数组b中off索引位置开始,写出到文件中
     */
    public static void FileOutputStreamTest() {
        // 待写入的字符串
        String str = "helloWord!";
        try {

            FileOutputStream fos = new FileOutputStream(new File("E:\\Desktop\\test01.txt"));
            byte[] ch = new byte[1024];
            ch = str.getBytes();
            fos.write(ch);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 使用 FileInputStream 读取本地文件
    /*
    文件字节输入流FileInputStream
    构造方法:
    (1)FileInputStream(File f):将一个File对象所表示的文件封装到一个字节输入流中,未来都是以字节的方式读取文件的内容
    (2)FileInputStream(String path):将一个字符串所表示的文件路劲封装到一个字节输入流中,未来都是以字节的方式读取文件的内容
    (3)注意事项,无论使用哪个构造方法,都只能封装文件的路径,封装文件夹路径没有任何意义,因为文件夹本身是没有任何数据的,所以也不能使用流对象读取数据

    常用方法:
    (1)int read():从当前输入流中,读取并返回一个字节,返回值类型是int,表示读取到的字节对应的整数结果,如果返回-1表示文件读取到末尾了
    (2)int read(byte[] arr):读取数组大小个数的字节信息,存储到数组中,返回值结果为int,表示本次读取到的字节的个数,如果读取到-1,表示读取完毕
    (3)void close():关闭流对象
     */
    public static void FileInputStreamTest() {
        // 读取文件
        try {
            FileInputStream fis = new FileInputStream(new File("E:\\Desktop\\test01.txt"));
            byte[] ch = new byte[1024];
            int length = 0;
            while ((length = fis.read(ch)) != -1) {
                System.out.println(new String(ch, 0, length));
            }
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 使用FileWriter 将数据写入到本地磁盘
    public static void FileWriterTest() {
        try {
            FileWriter fw = new FileWriter(new File("E:\\Desktop\\test02.txt"));
            fw.write(new String("这是第二个"));
            fw.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    // 使用FileReader 读取本地文件
    public static void FileReaderTest() {
        String pathname = "E:\\Desktop\\helloWord.txt";
        try {
            FileReader fr = new FileReader(new File("E:\\Desktop\\test02.txt"));
            char[] ch = new char[1024];
            int length = 0;
            while ((length = fr.read(ch)) != -1) {
                System.out.println(new String(ch, 0, length));
            }
            fr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

输入/输出流体系

处理流的用法

处理流可以隐藏底层设备上节点流的差异,并对外提供更加方便的输入/输出方法,让程序员更加关心高级流的操作。使用处理流的典型思路:使用处理流来包装节点流,程序通过处理流来执行输入/输出功能, 实际识别处理流非常简单,只要流的构造器参数不是一个物理节点,而是已经存在的流,那么这种流一定是处理流;而所有的节点流都是直接以物理IO节点作为构造器参数的。
例如使用PrintStream处理流来包装OutputStream:

public static void PrintStreamTest() {
        try {
            FileOutputStream fos = new FileOutputStream("E:\\Desktop\\test03.txt");
            PrintStream ps = new PrintStream(fos);
            ps.println("包装流输出的!");
            ps.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
}

image

参考链接:https://blog.csdn.net/yt_19940616/article/details/90705123

posted @ 2022-09-18 10:52  DevourZuan  阅读(31)  评论(0编辑  收藏  举报