Java 处理流--缓冲流(Buffered)

处理流:

作用:增强功能、提供性能,节点流之上
1.缓冲流
输入流:BufferedInputStream(字节) BufferedReader(字符)

输出流:BufferedOutputStream(字节) BufferedWriter(字符)


2、转换流: 字节流 转为字符流   处理乱码(编码集、解码集)
1)、编码与解码概念
 编码: 字符  ---编码字符集>二进制
 解码:二进制  --解码字符集->      字符
2)、乱码:
(1)编码与解码的字符集不统一
(2)字节缺少,长度丢失
(3)文件乱码
InputStreamReader(字节输入流,"解码集")
OutputStreamWriter(字符输出流,"编码集")

一、缓冲流

【1】对I/O进行缓冲是一种常见的性能优化,缓冲流为I/O流增加了内存缓冲区,增加缓冲区的两个目的:
(1)允许Java的I/O一次不只操作一个字符,这样提高整个系统的性能;
(2)由于有缓冲区,使得在流上执行skip、mark和reset方法都成为可能。

【2】缓冲流:它是要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,
提高了读写的效率,同时增加了一些新的方法。例如:BufferedReader中的readLine方法,
BufferedWriter中的newLine方法。

1.字节缓冲流:

将InputStream和OutputStream用Buffered包装起来。

package SAMPLE.IO流;
import java.io.*;
/*字节流实现文件拷贝
   1、选择文件:源文件src,目标文件dest。
   2、选择流:
       src-->InputStream
       dest-->OutputStream
   3、操作(拷贝):
       数据以字节流byte[]形式从src经过流流入dest,拷贝完成。
   4、释放资源
 */
public class 字节流实现文件拷贝 {
    public static void main(String[] args) {
        String srcPath = "C:\\Users\\kvnoe\\Desktop\\Jcase\\justice.jpg";
        String destPath = "C:\\Users\\kvnoe\\Desktop\\Jcase\\111.jpg";
        copy(srcPath,destPath);
    }
    public static void copy(String srcPath,String destPath){
        File src = new File(srcPath);
        File dest = new File(destPath);

        try(InputStream is = new BufferedInputStream(new FileInputStream(src));
            OutputStream os = new BufferedOutputStream(new FileOutputStream(dest,true))) {
            int len =-1;
            byte[] flush = new byte[1024];
            while((len=is.read(flush))!=-1){
                os.write(flush,0,len);
            }
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2.字符缓冲流:

相比于字符流新增了readLine()、newLine()方法。

package A;
import java.io.*;
/*字节流实现文件拷贝
   1、选择文件:源文件src,目标文件dest。
   2、选择流:
       src-->Reader
       dest-->Writer
   3、操作(拷贝)
   4、释放资源
 */
public class 字符流实现文件拷贝 {
    public static void main(String[] args) {
        String srcPath = "C:\\Users\\kvnoe\\Desktop\\Jcase\\justice.jpg";
        String destPath = "C:\\Users\\kvnoe\\Desktop\\Jcase\\111.jpg";
        copy(srcPath,destPath);
    }
    public static void copy(String srcPath,String destPath){
        File src = new File(srcPath);
        File dest = new File(destPath);
        try(Reader is = new BufferedReader(new FileReader(src));
            Writer os = new BufferedWriter(new FileWriter(dest,true))) {
            //新增操作方法
            String line =null;
            while(null!=(line= ((BufferedReader) is).readLine())){
                os.write(line);
                //wr.append("\r\n");
                ((BufferedWriter) os).newLine(); //换行符号
            }
            os.flush();//强制刷出
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值