java中提供了哪些流类?各种流类之间的关系是怎样的?什么场合需要使用什么流类
时间: 2024-05-30 17:11:01 浏览: 14
Java中提供了大量的流类,包括:
1. 字节流
- InputStream:字节输入流的基类
- OutputStream:字节输出流的基类
- FileInputStream:从文件中读取数据
- FileOutputStream:向文件中写入数据
- ByteArrayInputStream:从内存中读取数据
- ByteArrayOutputStream:向内存中写入数据
- BufferedInputStream:带缓冲的输入流
- BufferedOutputStream:带缓冲的输出流
- DataInputStream:可以读取基本数据类型的输入流
- DataOutputStream:可以写入基本数据类型的输出流
- ObjectInputStream:可以读取对象的输入流
- ObjectOutputStream:可以写入对象的输出流
2. 字符流
- Reader:字符输入流的基类
- Writer:字符输出流的基类
- FileReader:从文件中读取字符数据
- FileWriter:向文件中写入字符数据
- CharArrayReader:从字符数组中读取数据
- CharArrayWriter:向字符数组中写入数据
- BufferedReader:带缓冲的字符输入流
- BufferedWriter:带缓冲的字符输出流
- InputStreamReader:将字节流转换为字符流的桥梁
- OutputStreamWriter:将字符流转换为字节流的桥梁
各种流类之间的关系如下:
- 所有流类都是抽象类或接口
- InputStream 和 OutputStream 是所有字节流的基类
- Reader 和 Writer 是所有字符流的基类
- BufferedInputStream 和 BufferedOutputStream 是所有带缓冲的字节流的基类
- BufferedReader 和 BufferedWriter 是所有带缓冲的字符流的基类
- InputStreamReader 和 OutputStreamWriter 可以将字节流和字符流相互转换
使用哪种流类,取决于需要读写的数据类型和数据来源。如果是读写文本文件,建议使用字符流;如果是读写二进制文件,建议使用字节流。如果需要提高读写性能,可以使用带缓冲的流。如果需要读写基本数据类型或对象,可以使用 DataInputStream 和 DataOutputStream 或 ObjectInputStream 和 ObjectOutputStream。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)