BufferedOutputStream_字节缓冲输出流与BufferedInputStream_字节缓冲输入流

BufferedOutputStream——字节缓冲输出流

java.io.BufferedOutputStream extends OutputStream

 继承自父类的共性方法:

public void close() :关闭此输出流并释放与此流相关联的任何系统资源。

public void flush() :刷新此输出流并强制任何缓冲的输出字节被写出。

public void write(byte[] b ) :将b.length字节从指定的字节数组写入此输出流。

public void write(byte[] b,int off,int len) :从指定的字节数组写入len字节,从偏移量off开始输出到此输出流。

public abstract void write(int b) :将指定的字节输出流。

构造方法:

BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流,以将数据写入指定的底层输出流。

BufferedOutputStream(OutputStream out, int size)创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流

参数:

OutputStream out:字节输出流
我们可以传递FileOutputStream,缓冲流会给FileOutputStream增加一个缓冲区,提高FileOutputStream的写入效率

int size :指定缓冲流内部缓冲区的大小,不指定默认

 使用步骤:(重点)

1.创建FileOutputStream对象,构造方法中绑定要输出的目的地

2.创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象,提高FileOutputStream对象效率

3.使用BufferedOutputStream对象中的方法write,把数据写入到内部缓冲区中

4.使用BufferedOutputStream对象中的方法flush,把内部缓冲区中的数据,刷新到文件中

5.释放资源(会先调用flush方法刷新数据,第4步可以省略)

public class Demo_BufferedOutputStream01 {
    public static void main(String[] args) throws IOException {
        //1.创建FileOutputStream对象,构造方法中绑定要输出目的地
        FileOutputStream fos = new FileOutputStream("a.txt");
        //2.创建BufferedOutputStreom对象,构造方法中传递FiLeOutputStream对象对象,提高FiLeOutputStream对象效率
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //3.使用BufferedOutputStream对象中的方法write,把数据写入到内部缓冲区中
        bos.write("我把数据到内部缓存区中".getBytes());
        //4.使用BufferedOutputStream对象中的方法flush,把内部缓冲区中的数据,刷新到文件中
        bos.flush();
        //5.释放资源(会先调用flush方法刷新数据,第4步可以省略)
        bos.close();


    }

}

BufferedInputStream_字节缓冲输入流

package DemoOutputStream;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

/*
java.io.BufferedInputstream extends Inputstream
BufferedInputstream:字节缓冲输入流
继承自父类的成员方法:
int read()从输入流中读取数据的下一个字节。
int read(byte[] b)从输入流中读取一定数量的字节,并将其存储在缓冲区数组b 中。
void close()关闭此输入流并释放与该流关联的所有系统资源。
构造方法:
    BufferedInputStream(InputStream in)创建一个BufferedInputStream并保存其参数,即翰入流 in,以便将来使用。
    BufferedInputStream(InputStream in, int size)创建具有指定缓冲区大小的 BufferedInputStream并保存其参数,即输入流
    参数:
        Inputstream in :字节输入流
            我们可以传递FiLeInputstream,缓冲流会给FiLeInputStream增加一个缓冲区,提高FiLeInputStream的读取效率
        int size:指定缓冲内部缓冲区的大小,不指定默认
        使用步骤(重点):
1.创建FiLeInputstream对象,构造方法中绑定要读取的数据源
2.创建BufferedInputStream对象,构造方法中传递FiLeInputStream对象,,提高FiLeInputStrem对象的读取效率
3.使用BufferedInputstream对象中的方法read,读取文件
4.释放资源
 */
public class Demo_BufferedOutputStream02 {
    public static void main(String[] args) throws IOException {
        //1.创建FiLeInputstream对象,构造方法中绑定要读取的数据源
        FileInputStream fis = new FileInputStream("j.txt");
        //2.创建BufferedInputStream对象,构造方法中传递FiLeInputStream对象,,提高FiLeInputStrem对象的读取效率
        BufferedInputStream bis = new BufferedInputStream(fis);
        //3.使用BufferedInputstream对象中的方法read,读取文件
        //int read()从输入流中读取数据的下一个字节。
      /*
        int  len = 0;//记录每次读取到的字节
        while ((len = bis.read())!=-1){
            System.out.println(len);
        }
       */
        //int read(byte[] b)从输入流中读取一定数量的字节,并将其存储在缓冲区数组b 中。
        byte[] bytes = new byte[1024];//存储每次读取的数据
        int len = 0;//记录每次读取的有效字节个数
        while ((len=bis.read(bytes))!=-1){
            System.out.println(new String(bytes,0,len));
        }
        //4.释放资源
        bis.close();




    }
}

 

package DemoOutputStream;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

/*
java.io.BufferedInputstream extends Inputstream
BufferedInputstream:字节缓冲输入流
继承自父类的成员方法:
int read()从输入流中读取数据的下一个字节。
int read(byte[] b)从输入流中读取一定数量的字节,并将其存储在缓冲区数组b 中。
void close()关闭此输入流并释放与该流关联的所有系统资源。
构造方法:
BufferedInputStream(InputStream in)创建一个BufferedInputStream并保存其参数,即翰入流 in,以便将来使用。
BufferedInputStream(InputStream in int size)创建具有指定缓冲区大小的 BufferedInputStream并保存其参数,即输入流
参数:
Inputstream in :字节输入流
我们可以传递FiLeInputstream,缓冲流会给FiLeInputStream增加一个缓冲区,提高FiLeInputStream的读取效率
int size:指定缓冲内部缓冲区的大小,不指定默认
使用步骤(重点):
1.创建FiLeInputstream对象,构造方法中绑定要读取的数据源
2.创建BufferedInputStream对象,构造方法中传递FiLeInputStream对象,,提高FiLeInputStrem对象的读取效率
3.使用BufferedInputstream对象中的方法read,读取文件
4.释放资源
*/
public class Demo_BufferedOutputStream02 {
public static void main(String[] args) throws IOException {
//1.创建FiLeInputstream对象,构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("j.txt");
//2.创建BufferedInputStream对象,构造方法中传递FiLeInputStream对象,,提高FiLeInputStrem对象的读取效率
BufferedInputStream bis = new BufferedInputStream(fis);
//3.使用BufferedInputstream对象中的方法read,读取文件
//int read()从输入流中读取数据的下一个字节。
/*
int len = 0;//记录每次读取到的字节
while ((len = bis.read())!=-1){
System.out.println(len);
}
*/
//int read(byte[] b)从输入流中读取一定数量的字节,并将其存储在缓冲区数组b 中。
byte[] bytes = new byte[1024];//存储每次读取的数据
int len = 0;//记录每次读取的有效字节个数
while ((len=bis.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
//4.释放资源
bis.close();




}
}
posted @ 2022-07-12 13:31  zj勇敢飞,xx永相随  阅读(62)  评论(0编辑  收藏  举报