进阶day10-缓冲流的介绍
This commit is contained in:
36
javaSE-day10/src/com/inmind/buffered_stream01/Demo01.java
Normal file
36
javaSE-day10/src/com/inmind/buffered_stream01/Demo01.java
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package com.inmind.buffered_stream01;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
4个流本身是没有读写功能,都是基于昨天的字节流和字符流进行操作,所以,今天只要先区分到底是什么流,然后就调用对应流的api
|
||||||
|
|
||||||
|
缓冲流:基于字节字符流,它本身是没有读写功能,底层封装了一个8192长度的缓冲区(数组),提高读写效率,也叫高效流(空间换时间)
|
||||||
|
|
||||||
|
字节缓冲流:BufferedInputStream BufferedOutputStream
|
||||||
|
字符缓冲流:BufferedReader BufferedWriter
|
||||||
|
------------------------------------------------------------------------------------------------
|
||||||
|
字节缓冲流的基本使用以及使用缓冲流复制文件(一次读写一个字节)
|
||||||
|
字节缓冲输入流:BufferedInputStream
|
||||||
|
构造方法:
|
||||||
|
BufferedInputStream(InputStream in) 创建一个 BufferedInputStream并保存其参数,输入流 in供以后使用。
|
||||||
|
常用方法:
|
||||||
|
int read() :读取一个字节数据
|
||||||
|
int read(byte[] bytes) : 读取一个字节数组
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo01 {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
//IO流的操作
|
||||||
|
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));
|
||||||
|
|
||||||
|
//读取a.txt中的字节数据
|
||||||
|
int b ;//接收读取到的字节数据
|
||||||
|
while ((b = bis.read()) != -1) {
|
||||||
|
System.out.println((char) b);
|
||||||
|
}
|
||||||
|
bis.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user