大家好,我是哪吒。

创新互联主营鲁甸网站建设的网络公司,主营网站建设方案,重庆APP开发公司,鲁甸h5微信小程序开发搭建,鲁甸网站营销推广欢迎鲁甸等地区企业咨询
很多朋友问我,如何才能学好IO流,对各种流的概念,云里雾里的,不求甚解。用到的时候,现百度,功能虽然实现了,但是为什么用这个?不知道。更别说效率问题了~
下次再遇到,再百度,“良性循环”。
今天,我就用一天的时间,整理一下关于Java I/O流的知识点,分享给大家。
每一种IO流,都配有示例代码,大家可以跟着敲一遍,找找感觉~
InputStream 代表一个输入流,它是一个抽象类,不能被实例化。InputStream 定义了一些通用方法,如 read() 和 skip() 等,用于从输入流中读取数据。常用的 InputStream 实现类包括:
下面是使用 FileInputStream 读取文件内容的示例代码:
import java.io.*;
public class FileInputStreamExample {
    public static void main(String[] args) {
        // 要读取的文件路径和名称
        String filePath = "C:/example/file.txt";
        // 创建输入流对象
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(filePath);
            byte[] buffer = new byte[1024];
            int len;
            // 使用 while 循环读取文件,每次最多读取 1024 个字节
            while ((len = fis.read(buffer)) != -1) {
                // 将读取的字节转换为字符串,并输出到控制台
                String content = new String(buffer, 0, len, "UTF-8");
                System.out.println(content);
            }
        } catch (FileNotFoundException e) {
            System.out.println("File not found: " + filePath);
        } catch (IOException e) {
            System.out.println("Error reading file: " + e.getMessage());
        } finally {
            // 关闭输入流
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                System.out.println("Error closing file: " + e.getMessage());
            }
        }
    }
}示例代码说明:
下面是使用 ByteArrayInputStream 读取字节数组内容的示例代码:
import java.io.*;
public class ByteArrayInputStreamExample {
    public static void main(String[] args) {
        byte[] bytes = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 };
        // 创建字节输入流对象
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        try {
            byte[] buffer = new byte[1024];
            int len;
            // 使用 while 循环读取字节数组中的内容,每次最多读取 1024 个字节
            while ((len = bais.read(buffer)) != -1) {
                // 将读取的字节转换为字符串,并输出到控制台
                String content = new String(buffer, 0, len, "UTF-8");
                System.out.println(content);
            }
        } catch (IOException e) {
            System.out.println("Error reading byte array: " + e.getMessage());
        } finally {
            // 关闭输入流
            try {
                if (bais != null) {
                    bais.close();
                }
            } catch (IOException e) {
                System.out.println("Error closing byte array input stream: " + e.getMessage());
            }
        }
    }
}示例代码说明:
PipedInputStream:管道输入流,用于线程之间的通信。
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class PipedInputStreamExample {
    public static void main(String[] args) throws Exception {
        
        // 创建一对PipedInputStream和PipedOutputStream
        PipedInputStream input = new PipedInputStream();
        PipedOutputStream output = new PipedOutputStream(input);
        // 创建一个写线程
        Thread writerThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 写入一些数据到PipedOutputStream
                    output.write("Hello, World!".getBytes());
                    output.close(); // 关闭PipedOutputStream
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        // 创建一个读线程
        Thread readerThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 读取PipedInputStream中的数据
                    int data;
                    while ((data = input.read()) != -1) {
                        System.out.print((char) data); // 将数据打印到控制台
                    }
                    input.close(); // 关闭PipedInputStream
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        // 启动写线程和读线程
        writerThread.start();
        readerThread.start();
        // 等待写线程和读线程完成
        writerThread.join();
        readerThread.join();
    }
}PipedInputStream和PipedOutputStream是Java IO库提供的一对管道流,可以用于数据的发送和接收。
OutputStream 代表一个输出流,它也是一个抽象类,不能被实例化。OutputStream 定义了一些通用方法,如 write() 和 flush() 等,用于向输出流中写入数据。常用的 OutputStream 实现类包括:
文件输出流,用于向文件中写入数据。
import java.io.*;
public class FileOutputStreamExample {
    public static void main(String[] args) {
        // 要写入的文件路径和名称
        String filePath = "C:/example/output.txt";
        // 要写入文件的内容
        String content = "Hello, World!";
        // 创建输出流对象
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(filePath);
            // 将字符串转换为字节数组,并将其写入文件
            fos.write(content.getBytes("UTF-8"));
            // 刷新输出流
            fos.flush();
            // 输出提示信息
            System.out.println("Content has been written to " + filePath);
        } catch (FileNotFoundException e) {
            System.out.println("File not found: " + filePath);
        } catch (IOException e) {
            System.out.println("Error writing file: " + e.getMessage());
        } finally {
            // 关闭输出流
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                System.out.println("Error closing file: " + e.getMessage());
            }
        }
    }
}示例代码说明:
字节数组输出流,用于将数据写入内存中的字节数组中。
import java.io.*;
public class ByteArrayOutputStreamExample {
    public static void main(String[] args) {
        // 创建字节数组输出流对象
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            // 将字符串转换为字节数组,并写入到字节数组输出流中
            baos.write("Hello, World!".getBytes("UTF-8"));
            // 将字节数组输出流中的数据转换为字节数组
            byte[] bytes = baos.toByteArray();
            // 将字节数组转换为字符串,并输出到控制台
            String content = new String(bytes, "UTF-8");
            System.out.println(content);
        } catch (IOException e) {
            System.out.println("Error writing to byte array: " + e.getMessage());
        } finally {
            // 关闭字节数组输出流
            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                System.out.println("Error closing byte array output stream: " + e.getMessage());
            }
        }
    }
}示例代码说明:
管道输出流,用于线程之间的通信。
import java.io.*;
public class PipedOutputStreamExample {
    public static void main(String[] args) {
        // 创建一对PipedInputStream和PipedOutputStream
        PipedInputStream input = new PipedInputStream();
        PipedOutputStream output = new PipedOutputStream();
        try {
            // 将输入流和输出流连接起来
            input.connect(output);
            // 创建一个写线程
            Thread writerThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        // 写入一些数据到PipedOutputStream
                        output.write("Hello, World!".getBytes("UTF-8"));
                        // 刷新PipedOutputStream
                        output.flush();
                        // 关闭PipedOutputStream
                        output.close();
                    } catch (IOException e) {
                        System.out.println("Error writing to pipe: " + e.getMessage());
                    }
                }
            });
            // 创建一个读线程
            Thread readerThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        // 读取PipedInputStream中的数据
                        byte[] buffer = new byte[1024];
                        int len = input.read(buffer);
                        // 将读取的字节转换为字符串,并输出到控制台
                        String content = new String(buffer, 0, len, "UTF-8");
                        System.out.println(content);
                        // 关闭PipedInputStream
                        input.close();
                    } catch (IOException e) {
                        System.out.println("Error reading from pipe: " + e.getMessage());
                    }
                }
            });
            // 启动写线程和读线程
            writerThread.start();
            readerThread.start();
            // 等待写线程和读线程完成
            writerThread.join();
            readerThread.join();
        } catch (IOException | InterruptedException e) {
            System.out.println("Error communicating between threads: " + e.getMessage());
        }
    }
}示例代码说明:
除了字节流,Java 还提供字符流,字符流类似于字节流,不同之处在于字符流是按字符读写数据,而不是按字节。Java 中最基本的字符流是 Reader 和 Writer,它们是基于 InputStream 和 OutputStream 的转换类,用于完成字节流与字符流之间的转换。
常用的实现类包括 FileReader 和 InputStreamReader等
import java.io.FileReader;  // 引入 FileReader 类
import java.io.IOException; // 引入 IOException 类
public class FileReaderExample {
    public static void main(String[] args) {
        // 定义文件路径
        String filePath = "example.txt";
        try {
            // 创建 FileReader 对象
            FileReader fileReader = new FileReader(filePath);
            // 读取字符
            int character;
            while ((character = fileReader.read()) != -1) {
                // 打印字符
                System.out.print((char) character);
            }
            // 关闭 FileReader 对象
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}import java.io.BufferedReader; // 引入 BufferedReader 类
import java.io.IOException;    // 引入 IOException 类
import java.io.InputStreamReader; // 引入 InputStreamReader 类
public class InputStreamReaderExample {
    public static void main(String[] args) {
        try {
            // 创建 InputStreamReader 对象
            InputStreamReader inputStreamReader = new InputStreamReader(System.in);
            // 创建 BufferedReader 对象
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            // 获取用户输入
            System.out.println("请输入字符串:");
            String inputString = bufferedReader.readLine();
            // 打印用户输入
            System.out.println("您输入的字符串是:" + inputString);
            // 关闭 BufferedReader 对象
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}import java.io.*;
public class FileWriterExample {
    public static void main(String[] args) {
        FileWriter writer = null;
        try {
            writer = new FileWriter("example.txt");
            writer.write("Hello World!");
            writer.close();
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            try {
                if (writer != null)
                    writer.close();
            } catch (IOException e) {
                System.out.println("Error: " + e.getMessage());
            }
        }
    }
}示例代码说明:
import java.io.*;
public class OutputStreamWriterExample {
    public static void main(String[] args) {
        FileOutputStream outputStream = null;
        OutputStreamWriter writer = null;
        try {
            outputStream = new FileOutputStream("example.txt");
            writer = new OutputStreamWriter(outputStream, "UTF-8");
            writer.write("Hello World!");
            writer.close();
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            try {
                if (writer != null)
                    writer.close();
                if (outputStream != null)
                    outputStream.close();
            } catch (IOException e) {
                System.out.println("Error: " + e.getMessage());
            }
        }
    }
}示例代码说明:
BufferedInputStream 和 BufferedOutputStream 是 I/O 包中提供的缓冲输入输出流。它们可以提高 I/O 操作的效率,具有较好的缓存机制,能够减少磁盘操作,缩短文件传输时间。使用 BufferedInputStream 和 BufferedOutputStream 进行读取和写入时,Java 会自动调整缓冲区的大小,使其能够适应不同的数据传输速度。
import java.io.BufferedInputStream;   // 引入 BufferedInputStream 类
import java.io.BufferedOutputStream;  // 引入 BufferedOutputStream 类
import java.io.FileInputStream;        // 引入 FileInputStream 类
import java.io.FileOutputStream;       // 引入 FileOutputStream 类
import java.io.IOException;            // 引入 IOException 类
public class BufferedStreamsExample {
    public static void main(String[] args) {
        String sourceFile = "source.txt";
        String targetFile = "target.txt";
        try {
            // 创建 BufferedInputStream 和 BufferedOutputStream 对象
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(sourceFile));
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(targetFile));
            // 读取数据,直到读取的内容为-1
            int data;
            while ((data = bufferedInputStream.read()) != -1) {
                bufferedOutputStream.write(data);
            }
            // 关闭 BufferedInputStream 和 BufferedOutputStream 对象
            bufferedInputStream.close();
            bufferedOutputStream.close();
            // 打印成功信息
            System.out.println("复制文件成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}可以读取或写入 Java 对象的流,比较典型的对象流包括ObjectInputStream 和 ObjectOutputStream。
对象流需要将对象序列化和反序列化为字节序列,使用 ObjectInputStream 和 ObjectOutputStream 可以将 Java 对象转换为字节流进行传输或存储。
在网络传输和文件存储中,ObjectInputStream 和 ObjectOutputStream 通常会被使用到。
import java.io.FileInputStream;          // 引入 FileInputStream 类
import java.io.FileOutputStream;         // 引入 FileOutputStream 类
import java.io.ObjectInputStream;        // 引入 ObjectInputStream 类
import java.io.ObjectOutputStream;       // 引入 ObjectOutputStream 类
import java.io.Serializable;             // 引入 Serializable 接口
class Person implements Serializable {
    String name;
    int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String toString() {
        return "姓名:" + name + "\n年龄:" + age;
    }
}
public class ObjectStreamsExample {
    public static void main(String[] args) {
        String filePath = "person.dat";
        // 创建 Person 对象
        Person person = new Person("Alice", 20);
        try {
            // 创建 ObjectOutputStream 对象
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(filePath));
            // 将 Person 对象写入文件
            objectOutputStream.writeObject(person);
            // 关闭 ObjectOutputStream 对象
            objectOutputStream.close();
            // 创建 ObjectInputStream 对象
            ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(filePath));
            // 从文件中读取 Person 对象
            Person personFromFile = (Person) objectInputStream.readObject();
            // 关闭 ObjectInputStream 对象
            objectInputStream.close();
            // 打印读取的对象
            System.out.println(personFromFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}本文转载自微信公众号「哪吒编程」,可以通过以下二维码关注。转载本文请联系哪吒编程公众号。