处理流_缓冲流

1 和节点流相比,可以提升文件操作的效率

2 flush操作将最后缓存中剩余的字符串输出

package lianxi1;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.Test;

public class TestBuffered {
@Test
 public void testCopy(){
    long start = System.currentTimeMillis();
    String src = "d:/io/plan.png";
    String des = "prison.png";
    copyFile(src,des);
    long end = System.currentTimeMillis();
    System.out.println(end-start);
}
  public void copyFile(String src,String des){
    FileInputStream fis = null;
    FileOutputStream fos = null;
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    File file1 = new File(src);
    File file2 = new File(des);
    try{
        fis = new FileInputStream(file1);
        fos = new FileOutputStream(file2);
        bis = new BufferedInputStream(fis);
        bos = new BufferedOutputStream(fos);
        int len;
        byte[] b = new byte[100];
        while((len=bis.read(b))!=-1){
            bos.write(b, 0, len);
            bos.flush();
        }
    }catch(Exception ex){ex.printStackTrace();}
    finally{
        if(bis!=null){
            try {
                bis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(bos!=null){
            try {
                bos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
@Test
   public void test2(){
    FileReader fr = null;
    FileWriter fw = null;
    BufferedReader br = null;
    BufferedWriter bw = null;
    File file1 = new File("d:/io/hellotext.txt");
    File file2 = new File("Tangshan");
    try{
        fr = new FileReader(file1);
        fw = new FileWriter(file2);
        br = new BufferedReader(fr);
        bw = new BufferedWriter(fw);
//        int len;
//        char[] c = new char[100];
//        while((len=br.read(c))!=-1){
//            bw.write(c, 0, len);
//            bw.flush();
//        }
        //或用readline方法
        String str;
        while((str=br.readLine())!=null){
            bw.write(str);
            bw.newLine();
        }
    }catch(Exception ex){ex.printStackTrace();}
    finally{
        if(br!=null){
            try {
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(bw!=null){
            try {
                bw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
}
时间: 2024-08-08 22:00:36

处理流_缓冲流的相关文章

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

java缓冲流本身不具IO功能,只是在别的流上加上缓冲提高效率,像是为别的流装上一种包装.当对文件或其他目标频繁读写或操作效率低,效能差.这时使用缓冲流能够更高效的读写信息.因为缓冲流先将数据缓存起来,然后一起写入或读取出来.所以说,缓冲流还是很重要的,在IO操作时记得加上缓冲流提升性能. 缓冲流分为字节和字符缓冲流 字节缓冲流为: BufferedInputStream-字节输入缓冲流 BufferedOutputStream-字节输出缓冲流 字符缓冲流为: BufferedReader-字符

处理流之缓冲流 buffered

处理流是包在别的流上面的流,相当于包在别的管道上面的管道.(节点流是直接怼在数据源上的流) 缓冲流:缓冲流相当于自带小桶,对读写数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法.BufferedReader提供了一个一次读取一行的方法 readLine(),BufferedWriter提供了写入一个行分隔符的方法 newLine() BufferedReader(Reader in) (Reader in ,int size) BufferedWriter(Writer out)

【java的 IO流之缓冲流】

java缓冲流本身不具IO功能,只是在别的流上加上缓冲提高效率,像是为别的流装上一种包装.当对文件或其他目标频繁读写或操作效率低,效能差.这时使用缓冲流能够更高效的读写信息.因为缓冲流先将数据缓存起来,然后一起写入或读取出来.所以说,缓冲流还是很重要的,在IO操作时记得加上缓冲流提升性能. 缓冲流分为字节和字符缓冲流 字节缓冲流为: BufferedInputStream-字节输入缓冲流 BufferedOutputStream-字节输出缓冲流 字符缓冲流为: BufferedReader-字符

Java IO 过滤流 字节缓冲流 BufferedInput/OutputStream

Java IO 过滤流 字节缓冲流 BufferedInput/OutputStream @author ixenos 概念 BufferedInput/OutputStream是实现缓存的过滤流,他们分别是FilterInput/OutputStream的子类. BufferedInputStream工作流程 stream-->buf--read buf-->I 1.当一个BufferedInputStream被创建时,一个内部的缓冲区 byte[] buf = new byte[size]

JAVA输入/输出流(字节流、字符流、缓冲流)

JAVA输入/输出流 前期知识准备 1.基本java语法 基本原理: 程序在运行期间,可能需要从外部的存储媒介或其他程序中读入所需要的数据,这就需要使用输入流对象.输入流的指向称作它的源,程序从指向源的输入流中读取源中数据.另一方面,程序在处理数据后,可能需要将处理结果写入到永久的存储媒介中或传给其他应用程序,这就需要使用输出流对象.输出流的指向称作它的目的地,程序通过向输出流中写入数据把数据传送到目的地. (本博文只给出文件字节流,文件字符流,缓冲流实例) 文件字节流: FileInputSt

IO流之缓冲流

缓冲流 Java中提高了一套缓冲流,它的存在,可提高IO流的读写速度 缓冲流,根据流的分类字节缓冲流与字符缓冲流. 字节缓冲流 字节缓冲流根据流的方向,共有2个 l  写入数据到流中,字节缓冲输出流 BufferedOutputStream l  读取流中的数据,字节缓冲输入流 BufferedInputStream 它们的内部都包含了一个缓冲区,通过缓冲区读写,就可以提高了IO流的读写速度 字节缓冲输出流BufferedOutputStream 通过字节缓冲流,进行文件的读写操作 写数据到文件

IO流----转换流、缓冲流

打开一个文本文件,另存为: Ansi就是系统默认编码(就是gbk) 建一个编码是utf-8的txt文件, 例: import java.io.FileWriter; import java.io.IOException; public class Demo01 { public static void main(String[] args) throws IOException { //确定目的地 FileWriter fw=new FileWriter("E:\\zyx\\java\\utf-

Java转换流、缓冲流、流的操作规律和properties类整理

转换流 1.1                OutputStreamWriter类 OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流中的字符编码成字节.它的作用的就是,将字符串按照指定的编码表转成字节,在使用字节流将这些字节写出去. package com.oracle.reader; public class Demo04 { public static void main(String[] args) throws IOException

转换流和缓冲流

1. 转换流(把字符流转成了字节流所以是桥梁):OutputStreamWriter;是字符输出流;先用FileOutputStream确定要写入的地址;然后再用转换流抓取要写入的地址和码表 2.InputStreamReader:是FileReader的父类并且是它的转换流 3.InputStreamReader和OutputStreamWriter的转换原理(字节流+码表) 4.字符转成字节后再写入:明确目的地:FileOutputStream fos=new FileOutputStrea