一、基本的方法
1、read和write方法。(读/写字节,字节数组)
2、available可读入的字节数量。
3、close关闭流。
4、flush冲刷。(关闭的时候会进行冲刷。)
二、强大的方法
1、datainputstream/dataoutputstream
以二进制的格式读写所有的基本java类型。
2、Unicode文本
可以使用抽象类Reader和Writer的子类。
三、附加接口
1、Closeable
拥有close()方法。
2、Flushable
flush()方法。
3、Readable
read()方法。
4、Appendable
append(char c)添加单个字符。
append(CharSequence s)字符序列。
四、组合输入输出流过滤器
1、例子
//第一种
FileInputStream fin = new FileInputStream("employee");
DataInputStream din = new DataInputStream(fin);
double x = din.readDouble();
//第二种
DataInputStream din = new DataInputStream(
new BufferedInputStream(
new FileInputStream("employee.dat");
)
)
2、回推
//第一个应用,读入时,预览下一个。
PushbackInputStream pbin = new PushbackInputStream(
new BufferedInputStream(
new FileInputStream("employee.dat");
);
);
int b = pbin.read();//预读下一个字节。
if(b!='<')pbin.unread(b);//如果它并非你所期望的值时将其推回流中。
//第二个应用,预先浏览,读入数字。(回推输入流,数据输入流)
DataInputStream din = new DataInputStream(
pbin = new PushbackInputStream(
new BufferedInputStream(
new FileInputStream("employee.dat");
)
)
)
五、缓冲流
1、FileInputStream
FileInputStream(String name)
FileInputStream(File file)
(1)使用name或者file指定路径创建,新的文件输出流。
(2)非绝对路径时,将按照相对于VM启动时的工作目录来解析。
2、FileOutputStream
FileOutputStream(String name)/(String name ,boolean append)/(File file)/(File file,boolean append)
(1)使用name或者file创建一个新文件输出流。
(2)当append为true时,数据将被添加到文件尾,具有相同名字文件不会被删除,否则删除同名文件。
3、BufferedInputStream/BufferedOutputStream(OutputStream out)
BufferedInputStream(InputStream in)
(1)创建缓冲输入流/输出流。
(2)为空,读入新数据模块/输出流收集写出字符。
(3)带缓冲区的输入流在流中读入字符时,不会每次访问设备/当缓冲去填满或者被冲刷时,数据就被写出。
4、PushbackInputStream
PushbackInputStream(InputStream in)
PushbackInputStream(InputStream in,int size)
(1)构建一个可以预览的一个字节,指定尺寸的回推缓冲区输入流。
void unread(int b)
(2)回推一个字节,它可以在下次调用read时被再次获取。