InputStream(字节输入流)

类的声明为:

public abstract class InputStream extends Object implements Closeable

表示字节输入流的所有类的超类。

常用方法:

public void close() throws IOException:关闭此输入流并释放与该流关联的所有系统资源。 InputStream 的 close 方法不执行任何操作。

public abstract int read() throws IOException:从输入流中读取一个字节数据并返回该字节数据,如果到达流的末尾,则返回 -1。

public int read(byte[] buff) throws IOException:从输入流中读取多个字节数据,并存储在缓冲区数组 buff 中。返回已读取的字节数量,如果已到达流的末尾,则返回 -1。

OutputStream(字节输出流)

类的声明为:public abstract class OutputStream extends Object implements Closeable, Flushable,表示字节输出流的所有类的超类。

常用方法:

public void close() throws IOException:关闭此输出流并释放与此流有关的所有系统资源。

public abstract void write(int b) throws IOException:将指定的一个字节数据b写入到输出流中。

public void write(byte[] buff) throws IOException:把数组buff中所有字节数据写入到输出流中。

public void write(byte[] b, int off,int len) throws IOException:把数组buff中从索引off 开始的len 个字节写入此输出流中。

Reader(字符输入流)

类的声明为:

public abstract class Reader extends Object implements Readable, Closeable

表示字符输入流的所有类的超类。

常用方法:

public abstract void close() throws IOException:关闭此输入流并释放与该流关联的所有系统资源。

public int read() throws IOException:从输入流中读取一个字符数据并返回该字符数据,如果到达流的末尾,则返回 -1。

public int read(char[] cbuf) throws IOException:从输入流中读取多个字符,并存储在缓冲区数组 cbuf 中。返回已读取的字符数,如果已到达流的末尾,则返回 -1。

Writer(字符输出流)

类的声明为:

public abstract class Writer extends Object implements Appendable, Closeable, Flushable

表示字符输出流的所有类的超类。

常用方法:

public abstract void flush() throws IOException:刷新此输出流并强制写出所有缓冲的输出字符。

public abstract void close() throws IOException:关闭此输入流并释放与该流关联的所有系统资源。

public void write(int c) throws IOException:将指定的一个字符数据c写入到输出流中。

public void write(char[] cbuf) throws IOException:把数组cbuf中cbuf.length 个字符数据写入到输出流中。

public abstract void write(char[] cbuf, int off,int len) throws IOException:把数组cbuf中从索引off 开始的len 个字符写入此输出流中。

public void write(String str) throws IOException:将str字符串数据写入到输出流中。

文件流(重点)

当程序需要读取文件中的数据或者把数据保存到文件中去,此时就得使用文件流,但是注意只能操作纯文本文件(txt格式),不要使用Word、Excel。文件流比较常用。

需求1:使用文件字节输出流,把程序中数据保存到result1.txt文件,操作英文

private static void test1() throws Exception {
//1):创建源或者目标对象
File dest = new File("file/result1.txt");
//2):创建IO流对象
FileOutputStream out = new FileOutputStream(dest);
//3):具体的IO操作
out.write(65);//输出A
out.write(66);//输出B
out.write(67);//输出C
String str = "to be or not to be";
out.write(str.getBytes());//输出str字符串中所有内容
//4):关闭资源(勿忘)
out.close();
}

需求2:使用文件字节输入流,读取result1.txt文件中的数据

private static void test2() throws Exception {
//1):创建源或者目标对象
File src = new File("file/result1.txt");
//2):创建IO流对象
FileInputStream in = new FileInputStream(src);
//3):具体的IO操作
System.out.println((char)in.read());//读取A字节
System.out.println((char)in.read());//读取B字节
System.out.println((char)in.read());//读取C字节
byte[] buff = new byte[5];//准备容量为5的缓冲区
int len = in.read(buff);//读取5个字节数据,并存储到buff数组中
System.out.println(Arrays.toString(buff));//[116, 111, 32, 98, 101]
System.out.println(len);//返回读取了几个字节
//4):关闭资源(勿忘)
in.close();
}

需求3:使用文件字符输出流,把程序中数据保存到result2.txt文件,操作中文

private static void test3() throws Exception {
//1):创建源或者目标对象
File dest = new File("file/result2.txt");
//2):创建IO流对象
FileWriter out = new FileWriter(dest);
//3):具体的IO操作
out.write('辛');//输出A
out.write('弃');//输出B
out.write('疾');//输出C
String str = "众里寻他千百度,蓦然回首,那人却在,灯火阑珊处。";
out.write(str.toCharArray());
out.write(str);//String的本质就是char[]
//4):关闭资源(勿忘)
out.close();
}

需求4:使用文件字符输入流,读取result2.txt文件中的数据

private static void test4() throws Exception {
//1):创建源或者目标对象
File src = new File("file/result2.txt");
//2):创建IO流对象
FileReader in = new FileReader(src);
//3):具体的IO操作
System.out.println(in.read());//读取辛字符
System.out.println(in.read());//读取弃字符
System.out.println(in.read());//读取疾字符
char[] buff = new char[5];//准备容量为5的缓冲区
int len = in.read(buff);//读取5个字符数据,并存储到buff数组中
System.out.println(Arrays.toString(buff));//[众, 里, 寻, 他, 千]
System.out.println(len);//返回读取了几个字节
//4):关闭资源(勿忘)
in.close();
}

字节流和字符流选用问题

使用记事本打开某个文件,如果可以看到内容的就是文本文件,否则可以暂时认为是二进制格式的。

一般的,操作二进制文件(图片、音频、视频等)必须使用字节流。操作文本文件使用字符流,尤其是操作带有中文的文件,使用字符流不容易导致乱码,因为使用字节流可能出现读取半个汉字的尴尬(汉字由两个或三个字节组成)。当然,如果不清楚属于哪一类型文件,都可以使用字节流。