转换流

摘要:

InputStreamReader和OutputStreamWriter他们分别是FileReader和FileWriter的父类

当只是单纯的读写文件而不改变文件的编码格式时,就分别用他们的子类就可以

而当要改变编码格式不用系统默认编码格式(GBK)时,就需要用他们的父类来处理,父类提供了改变编码格式的方法。

OutputStreamWriter类

OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流中的字符编码成字节。

它的作用的就是,将字符串按照指定的编码表转成字节,在使用字节流将这些字节写出去。

public static void main(String[] args) throws IOException {
FileOutputStream fos=new FileOutputStream("e:\\test\\utf8.txt",true);
OutputStreamWriter osw=new OutputStreamWriter(fos,"utf-8");
osw.write("这是utf-8编码haha");
osw.close();
}

当我们调用OutputStreamWriter对象的write方法时,会拿着字符到指定的码表中进行查询,把查到的字符编码值转成字节数存放到OutputStreamWriter缓冲区中。

然后再调用刷新功能,或者关闭流,或者缓冲区存满后会把缓冲区中的字节数据使用字节流写到指定的文件中。

InputStreamReader类

public static void main(String[] args) throws IOException {
//明确数据源
FileInputStream fis=new FileInputStream("e:\\test\\utf8.txt");
InputStreamReader isr=new InputStreamReader(fis,"utf-8");
char[] ch=new char[1024];
int len=0;
while((len=isr.read(ch))!=-1){
System.out.println(new String(ch,0,len));
}
isr.close();
}

总结:

字节--->字符 : 看不懂的--->看的懂的。  需要读。输入流。 InputStreamReader

字符--->字节 : 看的懂的--->看不懂的。  需要写。输出流。 OutputStreamWriter

缓冲流

字节缓冲流

①写入数据到流中,字节缓冲输出流 BufferedOutputStream

②读取流中的数据,字节缓冲输入流 BufferedInputStream

它们的内部都包含了一个缓冲区,通过缓冲区读写,就可以提高了IO流的读写速度

字节缓冲输出流BufferedOutputStream

构造方法:

public BufferedOutputStream(OutputStream out)创建一个新的缓冲输出流,以将数据写入指定的底层输出流。

//缓冲字节输出流
public static void method1() throws IOException{
FileOutputStream fos=new FileOutputStream("e:\\test\\haha",true);
BufferedOutputStream bos=new BufferedOutputStream(fos);
//bos.write(100);
bos.write("你好啊".getBytes());
bos.close();
}

字节缓冲输入流 BufferedInputStream

构造方法:

public BufferedInputStream(InputStream in)

//缓冲字节输入流
public static void method2() throws IOException{
FileInputStream fis=new FileInputStream("e:\\test\\haha");
BufferedInputStream bis=new BufferedInputStream(fis);
int len=0;
while((len=bis.read())!=-1){
System.out.print((char)len);
}
bis.close();
}

实例:

对比字节流read(),字节流read(byte []),缓冲字节流read(),缓冲字节流read(byte [])它们的速度

//字节流read()
public static void method1() throws IOException{
long time1=System.currentTimeMillis();
FileInputStream fis=new FileInputStream("d:\\DW\\jdk\\javafx-src.zip");
FileOutputStream fos=new FileOutputStream("e:\\test\\text\\haha1.zip");
int len=0;
while((len=fis.read())!=-1){
fos.write(len);
}
long time2=System.currentTimeMillis();
System.out.println(time2-time1);
fis.close();
fos.close();
}
//字节流read(byte [])
public static void method2() throws IOException{
long time1=System.currentTimeMillis();
FileInputStream fis=new FileInputStream("d:\\DW\\jdk\\javafx-src.zip");
FileOutputStream fos=new FileOutputStream("e:\\test\\text\\haha2.zip");
int len=0;
byte[] bytes=new byte[1024];
while((len=fis.read(bytes))!=-1){
fos.write(bytes, 0, len);
}
long time2=System.currentTimeMillis();
System.out.println(time2-time1);
fis.close();
fos.close();
}
//缓冲字节流read()
public static void method3() throws IOException{
long time1=System.currentTimeMillis();
FileInputStream fis=new FileInputStream("d:\\DW\\jdk\\javafx-src.zip");
BufferedInputStream bis=new BufferedInputStream(fis);
FileOutputStream fos=new FileOutputStream("e:\\test\\text\\haha3.zip");
BufferedOutputStream bos=new BufferedOutputStream(fos);
int len=0;
while((len=bis.read())!=-1){
bos.write(len);
}
long time2=System.currentTimeMillis();
System.out.println(time2-time1);
fis.close();
fos.close();
}
//缓冲字节流read(byte [])
public static void method4() throws IOException{
long time1=System.currentTimeMillis();
FileInputStream fis=new FileInputStream("d:\\DW\\jdk\\javafx-src.zip");
BufferedInputStream bis=new BufferedInputStream(fis);
FileOutputStream fos=new FileOutputStream("e:\\test\\text\\haha4.zip");
BufferedOutputStream bos=new BufferedOutputStream(fos);
int len=0;
byte[] bytes=new byte[1024];
while((len=bis.read(bytes))!=-1){
bos.write(bytes, 0, len);
}
long time2=System.currentTimeMillis();
System.out.println(time2-time1);
fis.close();
fos.close();
}

结果:

文件大小:4m

字节流read() VS 字节流read(byte [])
150296毫秒 200毫秒
字节流read(byte [])完胜

缓冲字节流read() VS缓冲字节流read(byte [])
410毫秒 60毫秒
缓冲字节流read(byte [])完胜

速度总结:(慢——>快)

字节流read(),缓冲字节流read() ,字节流read(byte []),缓冲字节流read(byte [])

IO—》转换流和缓冲流的更多相关文章

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

    打开一个文本文件,另存为: Ansi就是系统默认编码(就是gbk) 建一个编码是utf-8的txt文件, 例: import java.io.FileWriter; import java.io.IO ...

  2. IO(转换流、缓冲流)

    第1章 转换流 在学习字符流(FileReader.FileWriter)的时候,其中说如果需要指定编码和缓冲区大小时,可以在字节流的基础上,构造一个InputStreamReader或者Output ...

  3. java:IO流(处理流(缓冲流,转换流,数据流),对象的序列化,Properties)

    字节缓冲流:(BufferedInputStream,BufferedOutStream) *按照流的功能来分:节点流和处理流 *节点流可以直接操作数据源: *InputStream *--FileI ...

  4. Java转换流、缓冲流、流操作规律整理

    转换流 1.1                OutputStreamWriter类 OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流中的字符编码成字 ...

  5. Java_转换流和缓冲流

    今日内容介绍 转换流 缓冲流 1 转换流 在学习字符流(FileReader.FileWriter)的时候,其中说如果需要指定编码和缓冲区大小时,可以在字节流的基础上,构造一个InputStreamR ...

  6. java基础(24):转换流、缓冲流

    1. 转换流 在学习字符流(FileReader.FileWriter)的时候,其中说如果需要指定编码和缓冲区大小时,可以在字节流的基础上,构造一个InputStreamReader或者OutputS ...

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

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

  8. IO流之缓冲流

    缓冲流 Java中提高了一套缓冲流,它的存在,可提高IO流的读写速度 缓冲流,根据流的分类字节缓冲流与字符缓冲流. 字节缓冲流 字节缓冲流根据流的方向,共有2个 l  写入数据到流中,字节缓冲输出流 ...

  9. java - >IO流_缓冲流(高效流)

    缓冲流(高效流) 在我们学习字节流与字符流的时候,大家都进行过读取文件中数据的操作,读取数据量大的文件时,读取的速度会很慢,很影响我们程序的效率,那么,我想提高速度,怎么办? Java中提高了一套缓冲 ...

随机推荐

  1. 在 Spring Boot 中,如何干掉 if else!

    需求 传统实现 策略模式实现 ClassScanner:扫描工具类源码 总结 需求 这里虚拟一个业务需求,让大家容易理解.假设有一个订单系统,里面的一个功能是根据订单的不同类型作出不同的处理. 订单实 ...

  2. (私人收藏)古风PPT高级灰蓝传艺

    古风PPT高级灰蓝传艺 https://pan.baidu.com/s/1ADgTfif8i6JqKORLXhTHHgn05p

  3. Django---drf入门

    目录 1 web开发模式 2 api接口 3 postman的使用 4 Restful规范(重点) 5 drf的安装和简单使用 3 cbv源码 4 APIView源码分析 1 web开发模式 #前后端 ...

  4. MVC引用asp.net报表(测试小例子)

    public class Default1Controller : Controller { // // GET: /Default1/ public ActionResult Index() { r ...

  5. WPF 2D纹理的准确映射

    TextureCoordinates定义了如何将一副2D纹理映射到所建立的3D网格上,TextureCoordinates为Positions集合中的每一个3D顶点提供了一个2D顶点. 映射时方向确定 ...

  6. NEST教程系列:推断索引名

    NEST教程系列:三种推断索引名写法 目录 NEST教程系列:三种推断索引名写法 连接时设置默认索引 设置 .NET 类映射索引名 在创建请求的时候直接显式指定索引名 总结 连接时设置默认索引 构建 ...

  7. matlab 打包exe

    mcc -m gui_abc.m https://blog.csdn.net/hujiameihuxu/article/details/53525373 deploytool app compiler

  8. Jmeter系列(43)- 详解 Jmeter 图形化 HTML 压测报告之 Charts 模块

    如果你想从头学习Jmeter,可以看看这个系列的文章哦 https://www.cnblogs.com/poloyy/category/1746599.html Charts 介绍 包含了各种详细信息 ...

  9. java 面向对象(三十五):泛型在继承上的体现

    泛型在继承上的体现: /* 1. 泛型在继承方面的体现 虽然类A是类B的父类,但是G<A> 和G<B>二者不具备子父类关系,二者是并列关系. 补充:类A是类B的父类,A< ...

  10. scrapy 基础组件专题(九):scrapy-redis 源码分析

    下面我们来看看,scrapy-redis的每一个源代码文件都实现了什么功能,最后如何实现分布式的爬虫系统: connection.py 连接得配置文件 defaults.py 默认得配置文件 dupe ...