流
- 在Java程序中,对于数据的输入/输出操作以“流”(stream)方式进行;J2SDK提供了各式各样的“流”类,用以获取不同种类的数据;程序中通过标准的方法输入或输出数据
1.Java流类的分类
- java.io包中定义了多个流类型(类或抽象类)来实现输入/输出功能;可以从不同的角度对其进行分类:
- 按数据流的方向不同可以分为输入流和输出流 【站在程序的角度】
- 按处理数据单位不同可以分为字节流和字符流
- 按照功能不同可以分为节点流和处理流
2.输入/输出流类
- 位于java.io内的所有流类型都分别继承自以下四种抽象流类型
字节流 | 字符流 | |
---|---|---|
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
1)InputStream
- 继承自InputStream的流都是用于向程序中输入数据,且数据的单位为字节(8bit)。【深色为节点流,浅色为处理流】
*InputStream的基本方法
- int read() throws IOException:读取一个字节并以整数的形式返回(0~255),如果返回-1即已到输入流的末尾
- int read(byte[] buffer) throws IOException:从输入流读取一些字节数,并将它们存储到缓冲区 buffer,返回实际读取的字节数,如果读取前已到输入流的末尾返回-1
- int read(byte[] buffer,int offset,int length) throws IOException:从输入流读取最多length字节的数据到一个字节数组buffer中。返回实际读取的字节数,如果读取前已到输入流的末尾返回-1
- void close() throws IOException:关闭流释放内存资源
- long skip(long n) throws IOException:跳过n个字节不读,返回实际跳过的字节数
2)OutputStream
- 继承自OutputStream的流是用于程序中输出数据,且数据的单位为字节(8bit)。【深色为节点流,浅色为处理流】
*OutputStream的基本方法
- void write(int b) throws IOException:向输出流中写入一个字节数据,该字节数据为参数b的第8位
- void write(byte[] b) throws IOException:将一个字节类型的数组中的数据写入输出流
- void write (byte[] buffer,int offset,int length) throws IOException:将一个字节类型的数组buffer中从指定位置offset开始的length个字节写入到输出流
- void close() throws IOException:关闭流释放内存资源
- void flush() throws IOException:将输出流中缓冲的数据全部写出到目的地【先写flush再写close】
3)Reader
- 继承自Reader的流都是用于向程序中输入数据,且数据的单位为字符(16bit)。【深色为节点流,浅色为处理流】
*Reader的基本方法
- int read() throws IOException:读取一个字符并以整数的形式返回(0~255),如果返回-1即已到输入流的末尾
- int read(char[] cbuf) throws IOException:读取一系列字符并存储到一个输出cbuf中,返回实际读取的字符数,如果读取前已到输入流的末尾返回-1
- int read(char[] cbuf,int offset,int length) throws IOException:从输入流读取最多length字符的数据到一个字节数组cbuf中。返回实际读取的字符数,如果读取前已到输入流的末尾返回-1
- void close() throws IOException:关闭流释放内存资源
- long skip(long n) throws IOException:跳过n个字符不读,返回实际跳过的字符数
4)Writer
- 继承自Writer的流都是用于向程序中输出数据,且数据的单位为字符(16bit)。【深色为节点流,浅色为处理流】
*Writer的基本方法
- void write(int c) throws IOException:向输出流中写入一个字符数据,该字符数据为参数c的第16位
- void write(char[] cbuf) throws IOException:将一个字符类型的数组中的数据写入输出流
- void write (char[] cbuf,int offset,int length) throws IOException:将一个字符类型的数组cbuf中从指定位置offset开始的length个字符写入到输出流
- void write(String str) throws IOException:讲一个字符串中的字符写入到输出流
- void write(String str,int offset,int length) throws IOException:将一个字符串从offset开始的length个字符写入到输出流
- void close() throws IOException:关闭流释放内存资源
- void flush() throws IOException:将输出流中缓冲的数据全部写出到目的地【先写flush再写close】
3.节点流和处理流
1)节点流
- 节点流为可以从一个特定的数据源(节点)读写数据(如:文件,内存)
类型 | 字符流 | 字节流 |
---|---|---|
File(文件) | FileReader;FileWriter | FileInputStream;FileOutputStream |
Memory Array | CharArrayReader;CharArrayWriter | ByteArrayInputStream;ByteArrayOutputStream |
Memory String | StringReader;StringWriter | — |
Pipe | PipedReader;PipedWriter | PipedInputStream;PipedOutPutStream |
2)处理流
- 处理流是“连接”在已存在的流(节点流或处理流)之上,通过数据的处理为程序提供更为强大的读写功能。【套在其他流之上的】
处理类型 | 字符流 | 字节流 |
---|---|---|
Buffering | BufferedReader;BufferedWriter | BufferedInputStream;BufferedOutputStream |
Filtering | FilterReader;FilterWriter | FilterInputStream;FilterOutputStream |
Converting between bytes and character | InputStreamReader;OutputStreamWriter | |
Object Serialization | — | ObjectInputStream;ObjectOutputStream |
Data conversion | — | DataInputStream;DataOutputStream |
Counting | LineNumberReader | LineNumberInputStream |
Peeking ahead | PusbackReader | PushbackInputStream |
Printing | PrintWriter | PrintStream |
4.文件流
1)FileInputStream和FileOutputStream
-
FileInputStream和FileOutputStream分别继承自InputStream和OutputStream用于向文件中输入和输出字节
-
FileInputStream和FileOutputStream类支持其父类InputStream和OutputStream所提供的的数据读写方法
-
注意:
①在实例化InputStream和OutputStream流时要用try-catch语句以处理其可能抛出的FileNotFoundException。
②在读写数据时也要用try-catch语句以处理可能抛出的IOException
③FileNotFoundException是IOException的子类
(1)FileInputStream举例
package com.stream;
/*
@author qw
@date 2020/8/6 - 22:08
**/
import java.io.*;
public class Demo01 {
public static void main(String[] args) {
int b = 0;
FileInputStream in = null;
try {
in = new FileInputStream("D:\\javaEx\\io\\TestFileInputStream.java");
} catch (FileNotFoundException e) {
System.out.println("找不到指定文件");
System.exit(-1);//系统退出
}
try {
long num = 0;
while ((b = in.read()) != -1) {
System.out.println((char) b);
num++;
}
in.close();
System.out.println();
System.out.println("共读取了" + num + "个字节");
} catch (IOException e1) {
System.out.println("文件读取错误");
System.exit(-1);
}
}
}
(2)FileOutputStream举例
package com.stream;
/*
@author qw
@date 2020/8/6 - 22:16
**/
import java.io.*;
public class Demo02 {
public static void main(String[] args) {
int b = 0;
FileInputStream in