缓冲流 Buffer :设置缓冲区加快执行效率

子类:

 (一)BufferedInputStream : 缓冲输入字节流 ,目的:提高读取文件的效率
   注意: BufferedInputStream 他是没有读写数据的功能
  内部实现 : 你面维护了一个8字节的byte数组。
  使用步骤:
        1.找到一个目标文件.
        2.建立通道 new FileInputStream(" ");
        3.创建一个缓冲字节输入流  new BufferedInputStream(FileInputStream);
        4.读取数据 read();
        5.关闭资源 close();

 (二)BufferedOutputStream :缓冲输出字节流    内部维护了一个 8k的字节数组
  作用: 提高文件的输出的效率,可以提供其他的方法。
  使用:
    1.找目标
    2.建立通道 FileOutputStream
    3.创建一个缓冲字节输出流
    4.写数据,不会写入到磁盘中。  如果数组中的数据已经满了,才会自动将数据写入到磁盘中。
    5.将数据写入到磁盘 : 调用flush(),或者关闭资源。
    6.关闭资源。

 (三)BuffredRead 缓冲输入字符流。
       有一个扩展的功能:readLine(); // 可以一次读一行文字。

 (四)BufferedWriter: 缓冲输出字符流
          内部提供一个8192长度的字符数组作为这样一个缓冲区。
      BufferedWriter作用 :提高写入的效率,拓展FileWriter的功能。

    newLine();  //换行写入数据

简单的字节缓冲流案例

 import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class buffered { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//bufInTest();
bufOutTest();
} //(1)BufferedInputStream 的使用
public static void bufInTest() throws IOException{
//1.找到目标
File file = new File("D:\\a.txt");
//2.创建通道
FileInputStream fis = new FileInputStream(file); //**3.创建一个缓冲输入字节流******
BufferedInputStream bfis = new BufferedInputStream(fis); //4.开始写入数据
int content = 0; // 一次只会取一个字节
while ((content= bfis.read())!= -1) { // 还是一个一个的读取 问什么可以提高效率呢?
System.out.print((char)content);
}
//5.关闭通道
bfis.close();
} //(2)BufferedOutputStream 的使用
public static void bufOutTest() throws IOException{ //1.找目标
File file = new File("D:\\b.txt");
//2.创建通道
FileOutputStream fos = new FileOutputStream(file); //3.创建缓冲流
BufferedOutputStream bfos = new BufferedOutputStream(fos); //4.创建写入文件的数据
String string = "good good study day day up"; //5.写数据, 到这一步只是将数据保存到内存中字节数组中。
bfos.write(string.getBytes()); //6.再刷新 将数据写入到磁盘中
//bfos.flush(); //7.关闭资源
bfos.close();//内部会实现flush();
}
}

简单的字符缓冲流案例

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; public class fileReader { public static void main(String[] args) throws IOException {
testBufferedWriter();
testBufferedRead();
}
//(1)缓冲输出字符流的使用
public static void testBufferedWriter() throws IOException{ //1.建立一个通道,指定一个路径
FileWriter fiw = new FileWriter("D:\\a.txt",true); //2.创建缓冲流
BufferedWriter bfw = new BufferedWriter(fiw); //让数据换行显示
bfw.newLine(); //换行写入数据 //3.开始写入数据
bfw.write("上课不要吃东西"); //4.关闭资源
bfw.close(); // 关闭资源之前会做一个刷新操作。调用flush()方法。
} //(2)缓冲输入字符流的使用
public static void testBufferedRead() throws IOException{ //1.创建通道并且指定路径
FileReader fir = new FileReader("D:\\b.txt"); //2.创建缓冲流
BufferedReader bfr = new BufferedReader(fir); //3.1、开始读取数据(一次读取一个字节)
int content = 0;
while ((content = bfr.read()) != -1) { System.out.print((char)content);
} //3.2、readLine()扩展功能,读取一行数据
String string1 = bfr.readLine();
System.out.println(string1); //一次读取一行数据(返回字符串),效率更高
String string = null;
while ((string = bfr.readLine()) != null) {
System.out.println(string);
} //4.关闭
bfr.close();
}
}

java 缓冲流 Buffer的更多相关文章

  1. Java缓冲流高效大文件的复制实例

    public class BufferedDemo { public static void main(String[] args) throws FileNotFoundException { // ...

  2. Java缓冲流细节

    FileOutPutStream继承OutputStream,并不提供flush()方法的重写所以无论内容多少write都会将二进制流直接传递给底层操作系统的I/O,flush无效果.而Buffere ...

  3. Java缓冲流的优点和原理

    不带缓冲的流的工作原理: 它读取到一个字节/字符,就向用户指定的路径写出去,读一个写一个,所以就慢了. 带缓冲的流的工作原理: 读取到一个字节/字符,先不输出,等凑足了缓冲的最大容量后一次性写出去,从 ...

  4. java 缓冲流

    english.txt The arrow missed the target. They rejected the union demand. Where does this road go to? ...

  5. Java缓冲流写出数据实例

    public class BufferedWriterDemo throws IOException { public static void main(String[] args) throws I ...

  6. java的 IO流之缓冲流(转载)

    java缓冲流本身不具IO功能,只是在别的流上加上缓冲提高效率,像是为别的流装上一种包装.当对文件或其他目标频繁读写或操作效率低,效能差.这时使用缓冲流能够更高效的读写信息.因为缓冲流先将数据缓存起来 ...

  7. Java I/O流操作(二)---缓冲流[转]

    转自:http://blog.csdn.net/johnny901114/article/details/8710403 一.BufferWriter类 IO的缓冲区的存在就是为了提高效率,把要操作的 ...

  8. Java IO流学习总结三:缓冲流-BufferedInputStream、BufferedOutputStream

    Java IO流学习总结三:缓冲流-BufferedInputStream.BufferedOutputStream 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/ ...

  9. java IO之 File类+字节流 (输入输出 缓冲流 异常处理)

    1. File类

随机推荐

  1. HiWorkV1.3版震撼公布,今日起正式公开測试!

    今天HiWork迎来了公开測试和V1.3大版本号更迭,HiWork集成的机器人达到20种,未读消息提醒亦可从不同维度进行设置,不断变好真是件振奋人心的事儿呢. 在这个看重颜值(kan lian)的互联 ...

  2. Ubuntu16.04 下python2 | python3

    在终端分别输入python,python2,python3 python和python2默认都是python2 python3才是python3 Ubuntu下是默认没有pip的,需要自己手动安装 s ...

  3. UIButton的图片和文字相对位置调整

    通常.假设直接设置UIButton的图片和文字,默认的两者相对位置可能不是我们想要的,那么须要进行调整. 须要用到的函数例如以下: UIEdgeInsetsMake(CGFloat top, CGFl ...

  4. android通过adb wireless的使用

    转自:http://www.cnblogs.com/Androider123/p/3848415.html?utm_source=tuicool 开发android程序,总是需要插拔插拔的,usb口都 ...

  5. Android修改签名

    #!/bin/shtmp=~/temp.apkcp "$1" "$tmp"zip -d "$tmp" META-INF/\*jarsigne ...

  6. HttpClient服务端发送http请求

    本来以为对跨域问题的处理已经比较熟练了.可以通过jsonp.document.domain+iframe.window.name.window.postMessage.服务器上设置代理页面来解决.但还 ...

  7. XMU C语言程序设计实践(1)

    题目: 任务1:英雄出世 炎热的夏天午后,小明正在百无聊赖地写c语言程序.忽然,电脑屏幕一阵抖动,浮现下面18×18个看似杂乱无章的数字: 32,  32,  32,  32,  32,  32,  ...

  8. leetcode 664. Strange Printer

    There is a strange printer with the following two special requirements: The printer can only print a ...

  9. publish and submit

    http://blog.csdn.net/w_jewelry/article/details/8123639 1.Gerrit里点击“publish and submit”提示如下:Your chan ...

  10. YTU 1002: Home Work

    1002: Home Work 时间限制: 1000 Sec  内存限制: 64 MB 提交: 288  解决: 41 题目描述 临近开学了,大家都忙着收拾行李准备返校,但I_Love_C却不为此担心 ...