缓冲流和转换流的使用【 File类+IO流知识回顾③】

2025-11-20 20:15:04   世界杯足球场

缓冲流

简单的说就是缓冲流在原有的流上进一步增强,读写效率更高。字节缓冲流:BufferedInputStream, BufferedOutputStream字符缓冲流:BufferedReader, BufferedWriter

字节缓冲流

BufferedInputStream 构造方法:BufferedOutputStream 构造方法:构造方法使用:

BufferedInputStream bis = new BufferedInputStream(new FileInputStream("文件位置"));

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("文件位置"));

字节缓冲流复制文件和普通输入输出流对比

public static void fileCopy1(String sourceFilePath,String targetFilePath){

// 1.创建输入输出流对象

FileInputStream fis = null;

FileOutputStream fos = null;

long start = System.currentTimeMillis(); // 复制开始起始时间

try{

fis = new FileInputStream(sourceFilePath);

fos = new FileOutputStream(targetFilePath);

// 2.定义len和byte数组

int len;

byte b[] = new byte[1024];

// 3.循环读入并且输出

while ((len=fis.read(b))!=-1){

fos.write(b,0,len);

}

}catch (IOException e){

System.out.println(e.getMessage());

}finally {

long end = System.currentTimeMillis(); // 复制结束时间

try{

if(fis!=null){

fis.close();

}

if(fos!=null){

fos.close();

}

}catch (IOException e){

System.out.println(e.getMessage());

}

long during = end - start;

System.out.println("copy完毕,耗时"+during);

}

}

public static void fileCopy2(String sourceFilePath,String targetFilePath) throws IOException {

// 创建流对象

FileInputStream fis = new FileInputStream(sourceFilePath);

FileOutputStream fos = new FileOutputStream(targetFilePath);

// 创建缓冲流对象

BufferedInputStream bis = new BufferedInputStream(fis);

BufferedOutputStream bos = new BufferedOutputStream(fos);

// 设置len和byte数组

int len;

byte b[] = new byte[1024];

long start = System.currentTimeMillis();

while ((len=bis.read(b))!=-1){

bos.write(b,0,len);

}

bis.close();

bos.close();

long end = System.currentTimeMillis();

System.out.println("copy完毕,耗时"+(end-start));

}

main方法测试,缓冲流比普通流快了3倍,这还是在复制185MB文件的情况下,

public static void main(String[] args) throws IOException {

// 测试文件地址 "D:\download\baidudisk\jdk-8u211-linux-x64.tar.gz" 185MB

String srcPath = "D:\\download\\baidudisk\\jdk-8u211-linux-x64.tar.gz";

String optPath = "D:\\jdk.tar.gz.bk";

// fileCopy1(srcPath,optPath); // copy完毕,耗时1101

fileCopy2(srcPath,optPath); // copy完毕,耗时337

}

字符缓冲流

BufferedReaderBufferedWriter构造方法使用:

BufferedReader bfr = new BufferedReader(new FileReader("文件位置"));

BufferedWriter bfw = new BufferedWriter(new FileWriter("文件位置"));

字符缓冲流的基本方法与普通字符流调用方式一致,但是字符缓冲流具备特有方法:

BufferedReader : public String readLine() 读取一行数据,读取到最后返回null

BufferedWriter:public void newLine() 换行,由系统属性定义符号

String filePath = "D:\\bfr.txt";

FileReader fr = null;

fr = new FileReader(filePath);

BufferedReader bfr = new BufferedReader(fr);

String readLine = null;

while ((readLine = bfr.readLine())!=null) {

System.out.println(readLine);

}

bfr.close();

String filePath = "D:\\bfw.txt";

BufferedWriter bfw = new BufferedWriter(new FileWriter(filePath));

// 先输出三次换行

bfw.newLine();

bfw.newLine();

bfw.newLine();

// 输出内容

bfw.write("中文。wwwwww");

bfw.close();

转换流

转换流即处理字节和字符的转换,也称之为编码和解码。编码:字符->字节解码:字节->字符

需要会使用的两个类 InputStreamReader 和 OutputStreamWriter ,从字面意思上不难理解,InputStreamReader 是 字节流到字符流的一个桥梁,OutputStreamWriter则是字符流到字节流的桥梁。

InputStreamReader

InputStreamReader是Reader的子类。

构造方法

方法名称

功能描述

public InputStreamReader(InputStream in)

创建一个使用默认字符集的字符流

public InputStreamReader(InputStream in, final String enc)

创建一个指定字符集的字符流

public InputStreamReader(InputStream in, CharsetDecoder dec)

创建一个指定解码字符集的字符流

public InputStreamReader(InputStream in, Charset charset)

创建一个指定字符集的字符流

利用转换流解决编码问题

编码问题的引入首先,写一个txt,编码方式为GB18030,然后尝试使用字符缓冲流进行读入。

String filePath = "D:\\bfr2.txt";

BufferedReader bfr = new BufferedReader(new FileReader(filePath));

String line = null;

while ((line = bfr.readLine())!=null){

System.out.println(line);

}

bfr.close();

从控制台窗口中看到,读取的字符都乱码了…此时为了应对不同编码文件的读入,我们就该使用字符转换流了。

下面是字符转换流的写法:

String filePath = "D:\\bfr2.txt";

InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "GBK");

int len ;

while ((len= isr.read())!=-1){

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

}

isr.close();

字符转换流完美的解决了因为编码不同导致乱码的问题。

OutputStreamWriter

OutputStreamWriter是Writer的子类,是字符流到字节流的桥梁,将字符输出流转换为字节输出流。使用指定的字符集将字符编码为字节。它的字符集可以由名称指定,也可以接受平台的默认字符集。

构造方法

public OutputStreamWriter(OutputStream out)

创建一个使用默认字符集的字符流

public OutputStreamWriter(OutputStream out, String charsetName)

创建一个指定字符集的字符流

public OutputStreamWriter(OutputStream out, Charset cs)

创建一个指定字符集的字符流

public OutputStreamWriter(OutputStream out, CharsetEncoder enc)

创建一个指定编码的字符流

指定编码输出文本

String optFilePath = "D:\\opsw.txt";

OutputStreamWriter opsw = new OutputStreamWriter(new FileOutputStream(optFilePath),"GBK");

opsw.write("中文-------编码GBK....");

opsw.write("123456");

opsw.write("abcdef");

opsw.close();

来开启新世界大门 哥们网《攻沙》转生玩法攻略
沈冰与世界杯的不解之缘:从央视女神到足球评论员的华丽转身