缓冲流:

  读取数据大量的文件时,读取的速度慢,java提供了一套缓冲流,提高IO流的效率;

  缓冲流分为字节缓冲流和字符缓冲流;

  字节输入缓冲流和字节输出缓冲流如下:

package com.zs.Demo;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class Demo {
public static void main(String[] args) {
try {
fun();
fun1();
} catch (IOException e) {
e.printStackTrace();
}
}
//字节缓冲输入流
private static void fun1() throws IOException {
//第二种创建缓冲流方式
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("d:\\a.txt"));
int len=0;
while((len=bis.read())!=-1){
System.out.println((char)len);
}
bis.close();
}
//字节缓冲输出流
private static void fun() throws IOException {
//第一种方式
FileOutputStream f=new FileOutputStream("d:\\a.txt");
BufferedOutputStream bos=new BufferedOutputStream(f);
//写入一个字节
bos.write(105);
bos.write("hello world".getBytes());//写入字节数组
bos.write("hello world".getBytes(),0,2);//写入字节数组指定内容
bos.close();
}
}

  字符输入缓冲流和输出缓冲流如下:

package com.zs.Demo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; public class Demo2 {
public static void main(String[] args) {
try {
fun();
fun1();
} catch (IOException e) {
e.printStackTrace();
}
}
//字符输出缓冲流
private static void fun() throws IOException {
BufferedWriter bw=new BufferedWriter(new FileWriter("d:\\b.txt"));
bw.write(111);//自动查码表,编码
bw.flush();//注意字符输出流每次操作都要刷新
bw.write("hello world".toCharArray());//写入字符数组
bw.flush();
bw.newLine();//newLine()特有方法,换行
bw.flush();
bw.write("java");//写入字符串
bw.flush();
bw.close();
}
//字符输入缓冲流
private static void fun1() throws IOException {
//字符输入缓冲流
BufferedReader br=new BufferedReader(new FileReader("d:\\b.txt"));
int len=0;
while ((len=br.read())!=-1) {
System.out.print((char)len);
}
br.close(); //创建字符数组输入缓冲流对象
BufferedReader br1=new BufferedReader(new FileReader("d:\\b.txt"));
int len1=0;
char[] c=new char[1024];
while((len1=br1.read(c))!=-1){
System.out.print(new String(c,0,len1));
}
br1.close(); //字符输入缓冲流特有的方法readLine() 一次读取一行
BufferedReader br2=new BufferedReader(new FileReader("d:\\b.txt"));
String len3=null;//这里注意是字符串类型
while((len3=br2.readLine())!=null){
System.out.println(len3);
}
br2.close();
}
}

下面写一个比较字节流,字节数组流,字节缓冲流,字节数组缓冲流复制文件速度的代码:

package com.zs.Demo;

import java.io.*;

public class Demo5 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
try {
//这里一个一个方法的运行,比较时间,复制的文件是一个240M的视频文件
// fun1("h:\\1.mp4","g:\\1.mp4");//字节流
// fun2("h:\\1.mp4","g:\\1.mp4");//字节数组流
// fun3("h:\\1.mp4","g:\\1.mp4");//字节缓冲流
fun4("h:\\1.mp4","g:\\1.mp4");//字节数组缓冲流
} catch (IOException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println(end-start);
}
//字节数组缓冲流
private static void fun4(String s, String s1) throws IOException {
BufferedInputStream fi=new BufferedInputStream(new FileInputStream(s));
BufferedOutputStream fo=new BufferedOutputStream(new FileOutputStream(s1));
int len=0;
byte[] b=new byte[1024*10];
while((len=fi.read(b))!=-1){
fo.write(b,0,len);
}//1653毫秒
fo.close();
fi.close();
}
//字节缓冲流
private static void fun3(String s, String s1) throws IOException {
BufferedInputStream fi=new BufferedInputStream(new FileInputStream(s));
BufferedOutputStream fo=new BufferedOutputStream(new FileOutputStream(s1));
int len=0;
while((len=fi.read())!=-1){
fo.write(len);
}//13015毫秒
fo.close();
fi.close();
}
//字节数组流
private static void fun2(String s, String s1) throws IOException {
FileInputStream fi=new FileInputStream(new File(s));
FileOutputStream fo=new FileOutputStream(new File(s1));
byte[] b=new byte[1024*10];
int len=0;
while((len=fi.read(b))!=-1){
fo.write(b,0,len);
}//6979毫秒
fo.close();
fi.close();
}
//字节流
private static void fun1(String s, String s1) throws IOException {
FileInputStream fi=new FileInputStream(new File(s));
FileOutputStream fo=new FileOutputStream(new File(s1));
int len=0;
while((len=fi.read())!=-1){
fo.write(len);
}//太慢了,等不下去了,时间十分钟以上
fo.close();
fi.close();
}
}

可以看出字节数组缓冲流速度最快,

java学习笔记30(IO :缓冲流)的更多相关文章

  1. Java学习笔记40(缓冲流)

    缓冲流: 在读写文件的各种流中,最令人烦恼的就是效率问题, 而缓冲流的目的就是提高读写效率 字节输出缓冲流: package demo; import java.io.BufferedOutputSt ...

  2. java学习笔记之IO编程—内存流、管道流、随机流

    1.内存操作流 之前学习的IO操作输入和输出都是从文件中来的,当然,也可以将输入和输出的位置设置在内存上,这就需要用到内存操作流,java提供两类内存操作流 字节内存操作流:ByteArrayOutp ...

  3. Java分享笔记:使用缓冲流复制文件

    [1] 程序设计 /*------------------------------- 1.缓冲流是一种处理流,用来加快节点流对文件操作的速度 2.BufferedInputStream:输入缓冲流 3 ...

  4. 【原】Java学习笔记033 - IO

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 需求:继承关系中爷 ...

  5. Java学习笔记之——IO

    一. IO IO读写 流分类: 按照方向:输入流(读),输出流(写) 按照数据单位:字节流(传输时以字节为单位),字符流(传输时以字符为单位) 按照功能:节点流,过滤流 四个抽象类: InputStr ...

  6. java学习笔记之IO编程—打印流和BufferedReader

    1.打印流(PrintWriter) 想要通过程序实现内容输出,其核心一定是要依靠OutputStream类,但是OutputStream类有一个最大缺点,就是这个类中的输出操作功能有限,所有的数据一 ...

  7. Java学习笔记43(打印流、IO流工具类简单介绍)

    打印流: 有两个类:PrintStream,PrintWriter类,两个类的方法一致,区别在于构造器 PrintStream:构造方法:接收File类型,接收字符串文件名,接收字节输出流(Outpu ...

  8. java学习笔记之IO编程—字节流和字符流

    1. 流的基本概念 在java.io包里面File类是唯一一个与文件本身有关的程序处理类,但是File只能够操作文件本身而不能操作文件的内容,或者说在实际的开发之中IO操作的核心意义在于:输入与输出操 ...

  9. Java学习笔记-10.io流

    1.输入流,只能从中读取数据,而不能向其写出数据.输出流,只能想起写入字节数据,而不能从中读取. 2.InputStream的类型有: ByteArrayInputStream 包含一个内存缓冲区,字 ...

随机推荐

  1. 『TensotFlow』RNN中文文本_下_暨研究生开学感想

    承前 接上节代码『TensotFlow』RNN中文文本_上, import numpy as np import tensorflow as tf from collections import Co ...

  2. 6月16 ThinkPHP连接数据库及Model数据模型层--------查询及数据添加

    连接数据库配置及Model数据模型层 convertion.php config.php 1.在config.php做数据库连接配置 2.修改配置 /* 数据库设置 */ 'DB_TYPE' => ...

  3. Mac OS下安装和配置MongoDB

    安装和配置教程: 参考地址:https://blog.csdn.net/yibowanbo/article/details/80233030 可视化管理工具: 地址:https://blog.csdn ...

  4. CO15批次确定,标准的太蛋疼了

    1.批次确定的配置,之前有转过,自己也动手配过,可以是可以,但是蛋疼,用户不愿意去弹出的界面选批次...2.因为这边的批次全部是按年月日+流水生成的,所以在批次确定这里就需要按照批次的号来排序选择了 ...

  5. Mac 安装md5sum等

    一.安装md5sum 方案1.使用brew 安装 方案2.使用源码编译安装 源码下载地址:http://www.microbrew.org/tools/md5sha1sum/md5sha1sum-0. ...

  6. Git的安装和创建版本库

    1.Git是分布式版本控制系统 2.安装Git 下载Git后,按照默认设置即可实现安装,安装完毕后点击git目录下的Git Bash 输入以下命令符: git config --global user ...

  7. PHPCMS V9完全开发介绍

    PHPCMS V9 文件目录结构: 根目录 | – api 接口文件目录 | – caches 缓存文件目录 | – configs 系统配置文件目录 | – caches_* 系统缓存目录 | – ...

  8. C#实体对象序列化成Json并让字段的首字母小写的两种解决方法

    引言:最近在工作中遇到与某些API对接的post的数据需要将对象的字段首字母小写.解决办法有两种:第一种:使用对象的字段属性设置JsonProperty来实现(不推荐,因为需要手动的修改每个字段的属性 ...

  9. 牛客网 PAT 算法历年真题 1009 : 1019. 数字黑洞 (20)

    1019. 数字黑洞 (20) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 给定任一个各位数字不完全相同的4 ...

  10. Spring Cloud之路:(七)SpringBoot+Shiro实现登录认证和权限管理

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sage_wang/article/details/79592269一.Shiro介绍1.Shiro是 ...