java字符流缓冲流_java之字符流、转换流、缓冲流

字符流

Writer(写文件) 字符输出流

Reader(读文件) 字符输入流

上面两个类是所有字符流的父类(抽象类)

基本的编码格式

mac 默认使用UTF-8格式(通用编码格式)

一个中文字符占3个字节

windows 默认使用GBK格式(简体中文格式)

一个中文字符占2个字节public class Demo04 {

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

//创建一个文件字符输出流

File file=new File("/Users/lanou/Desktop/Test/dongdong.txt");

FileWriter fw=new FileWriter(file);

//写

fw.write(65);

//刷新

//相当于写入的时候 有一个缓冲区

//写的字符实际上是先写入到缓冲区

//刷新时 才会将写的字符全部 写入到文件中

//建议:写一次刷新一次

fw.flush();

//利用字符数组写入

char[] c= {'a','s','d','f'};

fw.write(c);

fw.flush();

//利用字符串写入

fw.write("地方讲话稿\n");

fw.write("落实到发是\n");

fw.flush();

//关闭流资源

//关闭流资源之前会自动刷新到缓冲区

fw.close();

}

}

转换流

OutputSteamWriter(字符流转向字节流的桥梁)

1.程序中写入字符时 先使用转换流 根据转换流想查询码表格式去查询

2.如果查的是GBK格式 那么一个中文字符就查到了两个字节的字节编码

3.这个字节编码最后给到了构建转换流时 传入的字节流

4.通过这个字节流 按字节写入到文件中

转换流:可以查询对应的编码表

InputSteamReader(读取文件 字节流通向字符流的桥梁)

1.按字节读取文件 将字节编码给到转换流

2.如果UTF-8需要3个字节

3.使用字符流读取到程序中

按操作系统默认格式(utf-8) 写一个文件public static void getUTF8() throws IOException {

//创建一个字节输出流

FileOutputStream fos=new FileOutputStream("/Users/lanou/Desktop/Test/UTF8.txt");

//创建一个转换流

OutputStreamWriter osw=new OutputStreamWriter(fos);

//写文件

osw.write("张三");

//注意:

//1.一般只关外层的流就可以

//2.自己创建的流自己关 获取系统的流不用关

osw.close();

}

按默认UTF8格式读文件public static void readUTF8() throws IOException {

FileInputStream fis=new FileInputStream("/Users/lanou/Desktop/Test/UTF8.txt");

InputStreamReader isr=new InputStreamReader(fis);

//读

int len=0;

char[] c=new char[1024];

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

System.out.println(new String(c,0,len));

}

isr.close();

}

按GBK格式写入文件public static void getGBK() throws IOException {

FileOutputStream fos=new FileOutputStream("/Users/lanou/Desktop/Test/GBK.txt");

OutputStreamWriter osw=new OutputStreamWriter(fos,"GBK");

osw.write("张三");

osw.close();

}

按GBK格式读文件public static void readGBK() throws IOException {

FileInputStream fis=new FileInputStream("/Users/lanou/Desktop/Test/GBK.txt");

InputStreamReader isr=new InputStreamReader(fis,"GBK");

//读

int len=0;

char[] c=new char[1024];

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

System.out.println(new String(c,0,len));

}

isr.close();

}

缓冲流(高效流)

内部自带一个缓冲区(字节数组)

BufferedOutputSteam(写文件) 缓冲字节输出流

BufferedInputSteam(读文件) 缓冲字节输入流

写文件

private static void fun1() throws FileNotFoundException, IOException {

FileOutputStream fos=new FileOutputStream("/Users/lanou/Desktop/Test/guifen.txt");

//构建一个缓冲流写文件

BufferedOutputStream bos=new BufferedOutputStream(fos);

//写

bos.write("guifen".getBytes());

//关闭资源

bos.close();

}读文件

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

//fun1();

FileInputStream fis=new FileInputStream("/Users/lanou/Desktop/Test/guifen.txt");

BufferedInputStream bis=new BufferedInputStream(fis);

int len=0;

byte[] b=new byte[1024];

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

System.out.println(new String(b,0,len));

}

}测试缓冲流的高效性

public class Demo09 {

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

//测试

new Copy1().printTime();

//new Copy2().printTime();

}

}

//模板类

abstract class Test{

//声明复制文件路径成员变量

public String src="/Users/lanou/Desktop/Test/flappybird.mp4";

public String dest="/Users/lanou/Desktop/Test/aaa.mp4";

//计算程序时间的方法

public void printTime() throws IOException {

long start = System.currentTimeMillis();

//要计算程序

copyFile();

long stop = System.currentTimeMillis();

System.out.println(stop-start);

}

//抽象方法

//注意:异常在继承中的使用

public abstract void copyFile() throws IOException;

}

//文件字节流复制文件测试

class Copy1 extends Test{

@Override

public void copyFile() throws IOException {

//一个一个读

FileInputStream fis=new FileInputStream(src);

FileOutputStream fos=new FileOutputStream(dest);

//读

//int num=0;

//while ((num=fis.read())!=-1) {

//fos.write(num);

int len=0;

byte[] b=new byte[1024];

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

fos.write(b,0,len);

}

fis.close();

fos.close();

}

}

//缓冲流

class Copy2 extends Test{

@Override

public void copyFile() throws IOException {

FileInputStream fis=new FileInputStream(src);

BufferedInputStream bis=new BufferedInputStream(fis);

FileOutputStream fos=new FileOutputStream(dest);

BufferedOutputStream bos=new BufferedOutputStream(fos);

int len=0;

byte[] b=new byte[1024];

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

bos.write(b,0,len);

}

bis.close();

bos.close();

}

}//测试发现缓冲流要快几倍 体现高效性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值