Java定义了两种类型的流:字节流和字符流。当读取和写入二进制数据时,使用的就是字节流。字符流为处理字符的输入和输出提供了方便的方法。
(它们使用unicode编码,
所以可以被国际化)
- 字节流类
流类 | 含义 |
BufferedInputStream | 缓存的输入流 |
BufferedOutputStream | 缓存的输出流 |
ByteArrayInputStream | 读取字节数组内同的输入流 |
ByteArrayOutputStream | 向字节数组写入内容的输出流 |
DataInputStream | 包含读取Java标准数据类型的方法的输入流 |
DataOutputStream | 包含写入Java标准数据类型的方法的输出流 |
FileInputStream | 读取文件内容的输入流 |
FileOutputStream | 向文件写入内容的输出流 |
FilterInputStream |
实现InputStream
|
FilterOutputStream | 实现OutputStream |
InputStream | 描述流输入的抽象类(顶层类) |
ObjectInputStream | 用于对象的输入流 |
ObjectOutputStream | 用于对象的输出流 |
OutputStream | 描述流输出的抽象类(顶层类) |
PipedInputStream |
输入管道(用于线程之间的通信,对应唯一PipedOutputStream)
|
PipedOutputStream | 输出管道(用于线程之间的通信,对应唯一PipedInputStream) |
PrintStream | 包含print()和println()的输出流 |
PushbackInputStream |
支持1字节/多字节"unget"的输入流,这种流向输入流返回1个/多个字节
|
SequenceInputStream |
由两个或多个按顺序依次读取的输入流组合而成的输入流
|
- 字符流类
流类 | 含义 |
BufferedReader | 缓存的输入字符流 |
BufferedWriter | 缓存的输出字符流 |
CharArrayReader | 从字符数组读取内容的输入流 |
CharArrayWriter | 向字符数组写入内容的输出流 |
FileReader | 从文件读取内容的输入流 |
FileWriter | 向文件中写入内容的输出流 |
FilterReader | 过滤的读取器 |
FilterWriter | 过滤的写入器 |
InputStreamReader | 将字节转换成字符的输入流 |
LineNumberReader | 计算行数的输入流 |
OutputStreamWriter | 将字符转换为字节的输出流 |
PipedReader |
输入管道
(用于线程之间的通信,对应唯一PipedWriter)
|
PipedWriter | 输出管道(用于线程之间的通信,对应唯一PipedReader) |
PrintWriter | 包含print()和println()的输出流 |
PushbackReader |
允许字符返回到输入流的输入流
|
Reader | 描述字符流输入的抽象类(顶层类) |
StringReader | 从字符串读取内容的输入流 |
StringWriter | 向字符串写入内容的输出流 |
Writer | 描述字符流输出的抽象类(顶层类) |
- 读取控制台输入:
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- 读取字符:br.read() 如果达到流的末尾,就返回-1
- 读取字符串:br.readLine()
- 向控制台写输出:
- PrintWriter pw = new PrintWriter(System.out[, boolean]);
- 文件读写
- FileInputStream/FileOutputStream
- 读取文件:read() 从文件读取一个字节,当到达文件末尾时,read()方法返回-1
- 写入文件:write(int byteVal) 尽管byteVal被声明为整型,但只有低8位会被写入到文件中
- 文件使用完后必须关闭:close()
- 自动资源管理(带资源的try语句)
try(resource-specification){
// resource-specification是用来声明和初始化资源(例如文件流)的语句。
// 该语句包含一个变量声明,在该变量声明中使用将被管理的对象引用初始化变量。
// 当try代码块结束时,自动释放资源
// use the resource
}
-
- 只有对于那些实现了AutoCloseable接口的资源,才能使用带资源的try语句
- 示例:
/* This version of the ShowFile program uses a try-with-resources
statement to automatically close a file after it is no longer needed.
Note: This code requires JDK 7 or later.
*/
import java.io.*;
class ShowFile {
public static void main(String args[])
{
int i;
// First, confirm that a file name has been specified.
if(args.length != 1) {
System.out.println("Usage: ShowFile filename");
return;
}
// The following code uses a try-with-resources statement to open
// a file and then automatically close it when the try block is left.
try(FileInputStream fin = new FileInputStream(args[0])) {
do {
i = fin.read();
if(i != -1) System.out.print((char) i);
} while(i != -1);
} catch(FileNotFoundException e) {
System.out.println("File Not Found.");
} catch(IOException e) {
System.out.println("An I/O Error Occurred");
}
}
}
-
- 可以在一条try语句中管理多个资源。只需要简单地使用分号分隔每个资源约定即可。
- 示例:
/* A version of CopyFile that uses try-with-resources.
It demonstrates two resources (in this case files) being
managed by a single try statement.
*/
import java.io.*;
class CopyFile {
public static void main(String args[]) throws IOException
{
int i;
// First, confirm that both files has been specified.
if(args.length != 2) {
System.out.println("Usage: CopyFile from to");
return;
}
// Open and manage two files via the try statement.
try (FileInputStream fin = new FileInputStream(args[0]);
FileOutputStream fout = new FileOutputStream(args[1]))
{
do {
i = fin.read();
if(i != -1) fout.write(i);
} while(i != -1);
} catch(IOException e) {
System.out.println("I/O Error: " + e);
}
}
}
-
- 理解:在try语句中的资源被声明为隐式的final。这意味着在创建资源变量后,不能将其他资源赋给该变量。
- 注意:资源的作用域局限于带资源的try语句。