java 中四大基本流有什么联系
答案:2 悬赏:60
解决时间 2021-04-07 11:03
- 提问者网友:伴他一生,无悔
- 2021-04-06 18:10
java 中四大基本流有什么联系
最佳答案
- 二级知识专家网友:夢想黑洞
- 2021-04-06 19:00
InputStream\OutputStream\Reader\Writer构成了java.io的鼻祖。
具体如下:
InputStream和OutputStream类仅仅读取和写入单个的字节和字节数组,它们没有读取和
写入字符串和数值的方法。
对于Unicode文本,一个字符占用两个字节,所以出现了Reader和Writer。
特别:ZipInputSteam和ZipOutputStream能读写常见的ZIP压缩格式的文件。
InputStream、OutputStream、Reader、Writer类都实现了Closeable接口。
OutputStream和Writer都实现了Flushable接口。
FileInputStream和FileOutputStream能够把输入和输出流与磁盘文件关联起来。
eg:
FileInputStream fin = new FileInputStream("c:\\dev\\my.dat");------注意:最好使用反斜杠,反斜杠代表转义符使用。
或者:
File f = new File("c:\\dev\\my.dat');
FileInputStream fin = new FileInputStream (f);
用于读取字节:
byte b = (byte)fin.read();
用于读取数值:
DataInputStream dis = new DataInputStream(fin);
double d = dis.readDouble();
FileInputStream没有读取数值的能力,但是DataInputStream也没有从文件中读取数值的方法,所以两者注定在一起结合使用!
从上面我们可以知道:
java的处理IO的两种策略:一些流(如FileInputStream)可以从文件及其他地方接收字节;另一些流(如DataInputStream和PrintWriter)可以将字节组合成更加有用的数据类型。
eg:
FileInputStream fin = new FileInputStream("c:\\dev\\my.dat");---------从文件中读取值,创建流
DataInputStream dis = new DataInputStream(fin); --------结合后的流叫:过滤流(filtered stream)
double d = dis.readDouble();
默认情况下,流是不能进行缓冲处理的。就是每次对流进行read读取都要求操作系统发送一个新字节,若想要对当前目录下的文件
进行缓冲和数据输入操作,应该利用FileInputStream和FileOutputStream它们的子类合并为一个新的过滤流。
DataInputStream dis= new DataInputStream(new BufferedInputStream(new FileInputStream("c:\\dev\\my.dat")));
PushbackInputStream pis = new PushbackInputStream(new BufferedInputStream(new FileInputStream("c:\\dev\\my.dat")));-------能够对流进行跟踪,如下:
int b = pis.read(); -----读取下一个字节
if(b != '<') pis.unread(b);--------不是自己想要的,把它扔回去
但是read和unread是PushbackInputStream仅有的方法,若既想预查看,又想获取值,可以用:
DataInputStream dis= new DataInputStream(
pis = new PushbackInputStream(new BufferedInputStream(new FileInputStream("c:\\dev\\my.dat")));
利用构造的真正实用的流序列,也为我们带来很大的灵活性。
处理zip文件:
ZipInputStream zip = new ZipInputStream(new FileInputStream("ocean.zip"));
DataInputStream dis = new DataInputStream(zip);
具体如下:
InputStream和OutputStream类仅仅读取和写入单个的字节和字节数组,它们没有读取和
写入字符串和数值的方法。
对于Unicode文本,一个字符占用两个字节,所以出现了Reader和Writer。
特别:ZipInputSteam和ZipOutputStream能读写常见的ZIP压缩格式的文件。
InputStream、OutputStream、Reader、Writer类都实现了Closeable接口。
OutputStream和Writer都实现了Flushable接口。
FileInputStream和FileOutputStream能够把输入和输出流与磁盘文件关联起来。
eg:
FileInputStream fin = new FileInputStream("c:\\dev\\my.dat");------注意:最好使用反斜杠,反斜杠代表转义符使用。
或者:
File f = new File("c:\\dev\\my.dat');
FileInputStream fin = new FileInputStream (f);
用于读取字节:
byte b = (byte)fin.read();
用于读取数值:
DataInputStream dis = new DataInputStream(fin);
double d = dis.readDouble();
FileInputStream没有读取数值的能力,但是DataInputStream也没有从文件中读取数值的方法,所以两者注定在一起结合使用!
从上面我们可以知道:
java的处理IO的两种策略:一些流(如FileInputStream)可以从文件及其他地方接收字节;另一些流(如DataInputStream和PrintWriter)可以将字节组合成更加有用的数据类型。
eg:
FileInputStream fin = new FileInputStream("c:\\dev\\my.dat");---------从文件中读取值,创建流
DataInputStream dis = new DataInputStream(fin); --------结合后的流叫:过滤流(filtered stream)
double d = dis.readDouble();
默认情况下,流是不能进行缓冲处理的。就是每次对流进行read读取都要求操作系统发送一个新字节,若想要对当前目录下的文件
进行缓冲和数据输入操作,应该利用FileInputStream和FileOutputStream它们的子类合并为一个新的过滤流。
DataInputStream dis= new DataInputStream(new BufferedInputStream(new FileInputStream("c:\\dev\\my.dat")));
PushbackInputStream pis = new PushbackInputStream(new BufferedInputStream(new FileInputStream("c:\\dev\\my.dat")));-------能够对流进行跟踪,如下:
int b = pis.read(); -----读取下一个字节
if(b != '<') pis.unread(b);--------不是自己想要的,把它扔回去
但是read和unread是PushbackInputStream仅有的方法,若既想预查看,又想获取值,可以用:
DataInputStream dis= new DataInputStream(
pis = new PushbackInputStream(new BufferedInputStream(new FileInputStream("c:\\dev\\my.dat")));
利用构造的真正实用的流序列,也为我们带来很大的灵活性。
处理zip文件:
ZipInputStream zip = new ZipInputStream(new FileInputStream("ocean.zip"));
DataInputStream dis = new DataInputStream(zip);
全部回答
- 1楼网友:专属的偏见
- 2021-04-06 20:29
java中的流分为字节流和字符流,由下面四个抽象类表示 inputstream outputstream reader writer 其它的流的形式均由这四个基础抽象类派生
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯