---恢复内容开始---
一.流:一维单方向
1. 一维:即只需要给出一个参数就可以确定在流中的位置
2. 单方向:只能从一个方向流向另一个方向:要么是读,要么是写,不能在同一个流中读写并行。
那么想要既读又写,那么就需要开两个流。
例如:System.out.println()中的out就是一个可以用来做输出的流。
3. InputStream:从inputstream中按字节为单位读取,标记,作为返回值等。
1)int read() 、返回独到的文件的位置,若读到了末尾,返回-1;
read(byte[], int off, int len)、
read(byte b[])
2)int available()
3)mark()、reset()......
注:所有的read函数都是按照字节为单位进行读入的。
4. OutoutStream:
int write()
write(byte[])
write(byte[], int off, int len)
write的性质和read类似
二.文件流:FileInputStream and FileOutputStream
但凡是read或是write函数都是只能按字节为单位进行读取
1 //声明 2 FileOutputStream out = new FileOutputStream(filepath); 3 out.write(something); 4 out.close();
三.过滤器流:
1. 如果想要从文件中读取一个int, 或是String 或是其他, 而不只是byte的二进制数据,那么就要用到过滤器流
2. 在已有的文件流的基础上一层层的增加过滤器,我们可以利用这些过滤器实现那些我们可能需要的功能.
1 //通过DateOutputStream可以实现完成基础数据类型的写操作 2 //再将其存入到一层缓冲区流中 3 //最后通过文件流实现输出 4 DateOutputStream out = new DataOutputStream( 5 new BufferedOutputStream( 6 new FileOptputStream(filepath))); 7 int i = 0x666666; 8 out.writeInt(i); 9 out.close(); 10 //同理,我们可以实现一个基础数据类型的读入流: 11 //其实现原理正好为输出流的反向过程 12 DataInputStream in = new DataInputStream( 13 new BufferedInputStream( 14 new FileOptputStream(filepath))); 15 int j = in.readInt(); 16 System.out.println(j);
3. 文本输入输出:Reader and Writer(处理unicode 字符)
---恢复内容结束---
原文:https://www.cnblogs.com/-shuichi/p/10574270.html