java的无缓冲输出_java 缓冲流

Java中提高了一套缓冲流,它的存在,可提高IO流的读写速度

缓冲流,根据流的分类分类字节缓冲流与字符缓冲流。

一 字节缓冲流

字节缓冲流根据流的方向,共有2个

写入数据到流中,字节缓冲输出流 BufferedOutputStream

读取流中的数据,字节缓冲输入流 BufferedInputStream

它们的内部都包含了一个缓冲区,通过缓冲区读写,就可以提高了IO流的读写速度

1.字节缓冲输出流BufferedOutputStream

通过字节缓冲流,进行文件的读写操作 写数据到文件的操作

构造方法

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

public classBufferedOutputStreamDemo01 {public static void main(String[] args) throwsIOException {//写数据到文件的方法

write();

}/** 写数据到文件的方法

* 1,创建流

* 2,写数据

* 3,关闭流*/

private static void write() throwsIOException {//创建基本的字节输出流

FileOutputStream fileOut = new FileOutputStream("abc.txt");//使用高效的流,把基本的流进行封装,实现速度的提升

BufferedOutputStream out = newBufferedOutputStream(fileOut);//2,写数据

out.write("hello".getBytes());//3,关闭流

out.close();

}

}

2.字节缓冲输入流 BufferedInputStream

构造方法

public BufferedInputStream(InputStream in)

/** 从文件中读取数据

* 1,创建缓冲流对象

* 2,读数据,打印

* 3,关闭*/

private static void read() throwsIOException {//1,创建缓冲流对象

FileInputStream fileIn = new FileInputStream("abc.txt");//把基本的流包装成高效的流

BufferedInputStream in = newBufferedInputStream(fileIn);//2,读数据

int ch = -1;while ( (ch = in.read()) != -1) {//打印

System.out.print((char)ch);

}//3,关闭

in.close();

}

3.使用基本的流与高效的流完成复制文件

/** 需求:将d:\\test.avi文件进行复制

* 采用4种方式复制

* 方式1: 采用基本的流,一次一个字节的方式复制 共耗时 224613毫秒

* 方式2: 采用基本的流,一个多个字节的方式赋值 共耗时 327毫秒

* 方式3: 采用高效的流,一次一个字节的方式复制 共耗时 2047毫秒

* 方式4: 采用高效的流,一个多个字节的方式赋值 共耗时 96毫秒

*

* 数据源: d:\\test.avi

* 目的地1: d:\\copy1.avi

* 目的地2: d:\\copy2.avi

* 目的地3: d:\\copy3.avi

* 目的地4: d:\\copy4.avi

*

* 实现的步骤:

* 1,指定数据源

* 2,指定目的地

* 3,读数据

* 4,写数据

* 5,关闭流

**/

public classCopyAVI {public static void main(String[] args) throwsIOException {//开始计时

long start =System.currentTimeMillis();//方式1: 采用基本的流,一次一个字节的方式复制//method1("d:\\test.avi", "d:\\copy1.avi");//方式2: 采用基本的流,一个多个字节的方式赋值//method2("d:\\test.avi", "d:\\copy2.avi");//方式3: 采用高效的流,一次一个字节的方式复制//method3("d:\\test.avi", "d:\\copy3.avi");//方式4: 采用高效的流,一个多个字节的方式赋值

method4("d:\\test.avi", "d:\\copy4.avi");//结束计时

long end =System.currentTimeMillis();//打印耗时多少毫秒

System.out.println("共耗时 " +(end - start)+ "毫秒");

}//方式4: 采用高效的流,一个多个字节的方式赋值

private static void method4(String src, String dest) throwsIOException {//1,指定数据源

BufferedInputStream in = new BufferedInputStream(newFileInputStream(src));//2,指定目的地

BufferedOutputStream out = new BufferedOutputStream(newFileOutputStream(dest));//3,读数据

byte[] buffer = new byte[1024];int len = -1;while ( (len = in.read(buffer)) != -1) {//4,写数据

out.write(buffer, 0, le n);

}//5,关闭流

in.close();

out.close();

}//方式3: 采用高效的流,一次一个字节的方式复制

private static void method3(String src, String dest) throwsIOException {//1,指定数据源

BufferedInputStream in = new BufferedInputStream(newFileInputStream(src));//2,指定目的地

BufferedOutputStream out = new BufferedOutputStream(newFileOutputStream(dest));//3,读数据

int ch = -1;while ((ch=in.read()) != -1) {//4,写数据

out.write(ch);

}//5,关闭流

in.close();

out.close();

}//方式2: 采用基本的流,一个多个字节的方式赋值

private static void method2(String src, String dest) throwsIOException {//1,指定数据源

FileInputStream in = newFileInputStream(src);//2,指定目的地

FileOutputStream out = newFileOutputStream(dest);//3,读数据

byte[] buffer = new byte[1024];int len = -1;while ( (len=in.read(buffer)) != -1) {//4,写数据

out.write(buffer, 0, len);

}//5,关闭流

in.close();

out.close();

}//方式1: 采用基本的流,一次一个字节的方式复制

private static void method1(String src, String dest) throwsIOException {//1,指定数据源

FileInputStream in = newFileInputStream(src);//2,指定目的地

FileOutputStream out = newFileOutputStream(dest);//3,读数据

int ch = -1;while (( ch=in.read()) != -1) {//4,写数据

out.write(ch);

}//5,关闭流

in.close();

out.close();

}

}

二 字符缓冲流

字符缓冲输入流 BufferedReader

字符缓冲输出流 BufferedWriter

完成文本数据的高效的写入与读取的操作

1.字符缓冲输出流 BufferedWriter

将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。

方法:

void newLine() 根据当前的系统,写入一个换行符

/** BufferedWriter 字符缓冲输出流

* 方法

* public void newLine()写入一个行分隔符

*

* 需求: 通过缓冲输出流写入数据到文件

* 分析:

* 1,创建流对象

* 2,写数据

* 3,关闭流

**/

public classBufferedWriterDemo {public static void main(String[] args) throwsIOException {//创建流//基本字符输出流

FileWriter fileOut = new FileWriter("file.txt");//把基本的流进行包装

BufferedWriter out = newBufferedWriter(fileOut);//2,写数据

for (int i=0; i<5; i++) {

out.write("hello");

out.newLine();

}//3,关闭流

out.close();

}

}

2.字符缓冲输入流 BufferedReader

从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。

方法

public string readLine() 读取一个文本行,包含该行内容的字符串,不包含任何行终止符,如果已到达

流末尾,则返回 null

/** BufferedReader 字符缓冲输入流

*

* 方法:

* String readLine()

* 需求:从文件中读取数据,并显示数据*/

public classBufferedReaderDemo {public static void main(String[] args) throwsIOException {//1,创建流

BufferedReader in = new BufferedReader(new FileReader("file.txt"));//2,读数据//一次一个字符//一次一个字符数组//一次读取文本中一行的字符串内容

String line = null;while( (line = in.readLine()) != null){

System.out.println(line);

}//3,关闭流

in.close();

}

}

3.使用字符缓冲流完成文本文件的复制

/** 采用高效的字符缓冲流,完成文本文件的赋值

*

* 数据源: file.txt

* 目的地: copyFile.txt

*

* 分析:

* 1,指定数据源, 是数据源中读数据,采用输入流

* 2,指定目的地,是把数据写入目的地,采用输出流

* 3,读数据

* 4,写数据

* 5,关闭流*/

public classCopyTextFile {public static void main(String[] args) throwsIOException {//1,指定数据源, 是数据源中读数据,采用输入流

BufferedReader in = new BufferedReader(new FileReader("file.txt"));//2,指定目的地,是把数据写入目的地,采用输出流

BufferedWriter out = new BufferedWriter(new FileWriter("copyFile.txt"));//3,读数据

String line = null;while ( (line = in.readLine()) != null) {//4,写数据

out.write(line);//写入换行符号

out.newLine();

Out.flush();

}//5,关闭流

out.close();

in.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值