Java1.0中所有与输入有关的类都继承自InputStream,此类包含用于读取单个字节或字节数组的read()方法。所有与输出有关的类都继承自OutputStream,这个类包含用于写入单个字节或字节数组的write()方法。
1. InputStream类型
字节数组:ByteArrayInputStream, 内存缓冲区作为InputStream使用
String对象:StringBufferInputStream, 将一个String转化成InputStream的一个String,采用一个StringBuffer作为数据源
文件:FileInputStream,从文件中读取信息,将File或者FileDescriptor作为数据源
管道:PipedInputStream,为相关的PipedOutputStream提供数据源
连接:SequenceInputStream,将多个InputStream对象转化成单个InputStream使用
过滤器:FilterInputStream,过滤InputStream的数据源
2. OutputStream类型
衍生自这个类的类决定我们的输入去往何处
字节数组:ByteArrayOutputStream,内存中的缓冲区
文件:FileOutputStream,输出到文件
管道:PipedOutputStream,自动成为相关PipedInputStream的输出
过滤器:FilterOutputStream,过滤OutputStream的输出
3. FilterInputStream
FilterInputStream用于完成两类事情:读取不同的基本类型数据及String对象(DataInputStream),修改InputStream内部行为如是否进行缓冲
DataInputStream:与DataOutputStream联合使用,读取一个流中的基本数据类型
BufferedInputStream:预先读入缓冲,加快读取速度
LineNumberInputStream:跟踪输入流中的行号
PushbackInputStream:有一个字节的后推缓冲,可后推读入的上一个字符
4. FilterOutputStream
DataOutputStream:与DataInputStream配合使用,将基本数据类型写入OutputStream,使得任何机器上的DataInputStream都能正常读取
PrintStream:用于产生格式化输出
BufferedOutputStream:要求先写入缓冲区
5. RandomAccessFile
RandomAccessFile用于包含了已知长度记录的文件,我们可用seek()从一条记录移动至另一条,读取或修改记录。RandomAccessFile不属于InputStream OutputStream系统,而是直接继承自Object。
6. IO流的典型应用
package com.test;
import java.io.*;
public class IOTest {
public static void main(String[] args) {
try {
//Buffered input file
DataInputStream in = new DataInputStream(
new FileInputStream(args[0]));
String s, s2 = new String();
while ((s = in.readLine()) != null) {
s2 += s + "/n";
}
in.close();
//Input from memory
StringBufferInputStream in2 = new StringBufferInputStream(s2);
int c;
while ((c = in2.read()) != -1) {
System.out.print((char)c);
}
//Formatted memory input
try {
DataInputStream in3 = new DataInputStream(
new StringBufferInputStream(s2));
while (true) {
System.out.print((char)in3.readByte());
}
} catch (EOFException e) {
System.out.println("End of stream");
}
//Line numbering & file output
try {
LineNumberInputStream li = new LineNumberInputStream(
new StringBufferInputStream(s2));
DataInputStream in4 = new DataInputStream(li);
PrintStream out1 = new PrintStream(
new BufferedOutputStream(
new FileOutputStream("IODemo.out")));
while ((s = in4.readLine()) != null) {
out1.println("Line " + li.getLineNumber() + s);
}
out1.close();
} catch (EOFException e) {
System.out.println("End of Stream");
}
//Storing and recovering data
try {
DataOutputStream out2 = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("Data.txt")));
out2.writeBytes("Here's the value of pi: \n");
out2.writeDouble(3.141592);
out2.close();
DataInputStream in5 = new DataInputStream(
new BufferedInputStream(
new FileInputStream("Data.txt")));
System.out.println(in5.readLine());
System.out.println(in5.readDouble());
} catch (EOFException e) {
System.out.println("End of stream");
}
//Reading/writing radom access file
RandomAccessFile rf = new RandomAccessFile("rtest.dat", "rw");
for (int i = 0; i < 10; i++) {
rf.writeDouble(i*1.414);
}
rf.close();
rf = new RandomAccessFile("rtest.dat", "rw");
rf.seek(5*8);
rf.writeDouble(47.0001);;
rf.close();
rf = new RandomAccessFile("rtest.dat", "r");
for (int i = 0; i < 10; i ++) {
System.out.println("Value " + i + ": " + rf.readDouble());
}
rf.close();
} catch (FileNotFoundException e) {
System.out.println("File not found:" + args[0]);
} catch (IOException e) {
System.out.println("IO Exception");
}
}
}
Java1.1 IO System
Java1.1保留了原来的InputStream和OutputStream系统,但是引入了新的Reader和Writer类实现了另一中IO设计方案。这两种系统可通过桥类转换,InputStreamReader将InputStream转换成Reader,OutputStreamWriter将OutputStream转换成Writer。新IO系统中添加了与之前对应的类,如下:
Java1.0
Java1.1
InputStream
Reader
converter:InputStreamReader
OutputStream
Writer
converter:OutputStreamWriter
FileInputStream
FileReader
FileOutputStream
FileWriter
StringBufferInputStream
StringReader
没有对应类
StringWriter
ByteArrayInputStream
CharArrayReader
ByteArrayOutputStream
CharArrayWriter
PipedyInputStream
PipedReader
PipedyOutputStream
PipedWriter
过滤器:
Java1.0
Java1.1
FilterInputStream
FilterReader
FilterOutputStream
FilterWriter
BufferedInputStream
BufferedReader
含有readline()
BufferedOutputStream
BufferedWriter
DataInputStream
DataInputStream
用到readline()时需要BufferedReader
PrintStream
PrintWriter
LineNumberInputStream
LineNumberReader
StreamTokenizer
StreamTokenizer
PushBackInputStream
PushBackReader
为改变的类:
DataOutputStream
File
RandomAccessFile
SequenceInputStream