JAVA核心卷二——输入输出

一、基本的方法

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时被再次获取。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值