JAVA中的缓冲流、转换流、序列化流和打印流

字节缓冲流

构造方法

  • public BufferedInputStream(InputStream in) :创建一个新的缓冲输入流
  • public BufferedOutputStream(OutputStream out) : 创建一个新的缓冲输出流

例子:

public class Main {
    public static void main(String[] args) throws IOException {
        //创建缓存输入流对象
        BufferedInputStream bis = 
                new BufferedInputStream(new FileInputStream("a.txt"));
        //创建缓存输出流对象
        BufferedOutputStream bos=
                new BufferedOutputStream(new FileOutputStream("b.txt"));
    }
}

效率测试

查询API,缓冲流读写方法与基本的流是一致的,通过复制大文件,测试它的效率


基本流

public class Main {
    public static void main(String[] args) throws IOException {
        // 记录开始时间
        long start = System.nanoTime()/1000;
        // 创建流对象
        FileInputStream fis = new FileInputStream("a.txt");
        FileOutputStream fos = new FileOutputStream("copy.txt");

        // 读写数据
        int b;
        while ((b = fis.read()) !=-1) {
            fos.write(b);
        }

        // 记录结束时间
        long end = System.nanoTime()/1000;
        System.out.println("普通流复制时间:"+(end -start)+" 微秒");
    }
}

缓冲流

public class Main {
    public static void main(String[] args) throws IOException {
        // 记录开始时间
        long start = System.nanoTime()/1000;
        // 创建流对象
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));
        BufferedOutputStream bos= new BufferedOutputStream(new FileOutputStream("copy.txt"));

        // 读写数据
        int b;
        while ((b = bis.read()) !=-1) {
           bos.write(b);
        }

        // 记录结束时间
        long end = System.nanoTime()/1000;
        System.out.println("缓冲流复制时间:"+(end -start)+" 微秒");
    }
}

如何更快呢

采用数组存储数据的方式(大文件才看的出来)

public class Main {
    public static void main(String[] args) throws IOException {
        // 记录开始时间
        long start = System.nanoTime()/1000;
        // 创建流对象
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));
        BufferedOutputStream bos= new BufferedOutputStream(new FileOutputStream("copy.txt"));

        // 读写数据
        byte[] bytes=new byte[1024];
        while (bis.read(bytes) !=-1) {
            bos.write(bytes);
        }

        // 记录结束时间
        long end = System.nanoTime()/1000;
        System.out.println("缓冲流复制时间:"+(end -start)+" 微秒");
    }
}

字符缓冲流

构造方法

  • public BufferedReader(Reader in) :创建一个 新的缓冲输入流
  • public BufferedWriter(Writer out) : 创建一个新的缓冲输出流

例子

public class Main {
    public static void main(String[] args) throws IOException {
        // 创建字符缓冲输入流
        BufferedReader br = new BufferedReader(new FileReader("a.txt"));
        // 创建字符缓冲输出流
        BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));

    }
}

特有方法

  • BufferedReader: public String readLine() : 读一行文字
  • BufferedWriter: public void newLine() : 写一行行分隔符,由系统属性定义符号

例子:

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("a.txt"));
        String line=null;
        //一行一行的读取
        while ((line= br.readLine())!=null){
            System.out.println(line);
        }

        BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));
        bw.write("Hello");
        //换行
        bw.newLine();
        bw.write("Java");
        br.close();
        bw.close();
    }
}

转换流


字符编码

  计算机中储存的信息都是用二进制数表示的,而我们在屏幕上看到的数字、英文、标点符号、汉字等字符是二进制数转换之后的结果。按照某种规则,将字符存储到计算机中,称为编码 。反之,将存储在计算机中的二进制数按照 某种规则解析显示出来,称为解码 。比如说,按照A规则存储,同样按照A规则解析,那么就能显示正确的文本f符号。反之,按照A规则存储,再按照B规则解析,就会导致乱码现象。

ASCII字符集 :

  ASCII基于拉丁字母的一套电脑编码系统,用于显示现代英语,主要包括控制字符(回车键、退格、换行键等)和可显示字符(英文大小写字符、阿拉伯数字和西文符号)。
  基本的ASCII字符集,使用7位(bits)表示一个字符,共128字符。ASCII的扩展字符集使用8位(bits) 表示一个字符,共256字符,方便支持欧洲常用字符。

ISO-8859-1字符集:

   拉丁码表,别名Latin-1,用于显示欧洲使用的语言,包括荷兰、丹麦、德语、意大利语、西班牙语等。
   ISO-5559-1使用单字节编码,兼容ASCII编码。

GBxxx字符集:
GB就是国标的意思,是为了显示中文而设计的一套字符集。

   GB2312:简体中文码表。一个小于127的字符的意义与原来相同。但两个大于127的字符连在一起时,就表示一个汉字,这样大约可以组合了包含7000多个简体汉字,此外数学符号、罗马希腊的字母、日文的假名们都编进去了,连在ASCII里本来就有的数字、标点、字母都统统重新编了两个字节长的编码,这就是常说的"全角"字符,而原来在127号以下的那些就叫"半角"字符了。

   GBK:最常用的中文码表。是在GB2312标准基础上的扩展规范,使用了双字节编码方案,共收录了21003个汉字,完全兼容GB2312标准,同时支持繁体汉字以及日韩汉字等。

   GB18030:最新的中文码表。收录汉字70244个,采用多字节编码,每个字可以由1个、2个或4个字节组成。支持中国国内少数民族的文字,同时支持繁体汉字以及日韩汉字等。

Unicode字符集 :

   Unicode编码系统为表达任意语言的任意字符而设计,是业界的一种标准,也称为统一码、标准万国码。
  它最多使用4个字节的数字来表达每个字母、符号,或者文字。有三种编码方案,UTF-8、UTF-16和UTF32。最为常用的UTF-8编码。
  UTF-8编码,可以用来表示Unicode标准中任何字符,它是电子邮件、网页及其他存储或传送文字的应用中,优先采用的编码。互联网工程工作小组(IETF)要求所有互联网协议都必须支持UTF-8编码。

所以,我们开发Web应用,也要使用UTF-8编码。它使用一至四个字节为每个字符编码,编码规则:

  1. 128个US-ASCII字符,只需一个字节编码
  2. 拉丁文等字符,需要二个字节编码
  3. 大部分常用字(含中文),使用三个字节编码
  4. 其他极少使用的Unicode辅助字符,使用四字节编码

InputStreamReader

构造方法

  • InputStreamReader(InputStream in) : 创建一个使用默认字符集的字符流
  • InputStreamReader(InputStream in, String charsetName) : 创建一个指定字符集的字符流

例子:

public class Main {
    public static void main(String[] args) throws IOException {
        //创建一个使用默认字符集的字符流
        InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));

        //创建指定字符集的字符流
        InputStreamReader isr1=new InputStreamReader(new FileInputStream("a.txt"),"GBK");
    }
}

OutputStreamWriter

构造方法

  • OutputStreamWriter(OutputStream in) : 创建一个使用默认字符集的字符流
  • OutputStreamWriter(OutputStream in, String charsetName) :创建一个指定字符集的字符流

例子:

public class Main {
    public static void main(String[] args) throws IOException {
        // 创建使用默认字符集的字符流
        OutputStreamWriter isr = new OutputStreamWriter(new FileOutputStream("b.txt"));
        //创建指定字符集的字符流
        OutputStreamWriter isr2 = new OutputStreamWriter(new FileOutputStream("b.txt") , "GBK");
    }
}

在这里插入图片描述

练习

将UTF-8的文本文件转换为GBK的文本文件

public class Main {
    public static void main(String[] args) throws IOException {
       InputStreamReader isr=new InputStreamReader(new FileInputStream("a.txt"));
       OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("b.txt"),"GBK");
       int len=0;
       while ((len=isr.read())!=-1){
           osw.write(len);
       }
       osw.close();
       isr.close();
    }
}

序列化

   序列化将Java对象持久化的存储在硬盘上,反序列化将Jav存储在硬盘上Java对象读取回来。

ObjectOutputStream

构造方法

  • public ObjectOutputStream(OutputStream out) :根据输出流创建序列化对象

写出对象方法

  • public final void writeObject (Object obj) : 将指定的对象写出

序列化操作的前提条件

  1. 实现 java.io.Serializable 接口
  2. 该类的所有属性必须是可序列化的。如果有一个属性不需要可序列化的,则该属性必须注明是瞬态的,使用transient 关键字修饰。

例子

public class Student implements Serializable {
    public String name;
    public transient int age;//被transient修饰不会被序列化

    public void eat(){
        System.out.println("开吃了");
    }
}
 public class Main {
    public static void main(String[] args) throws IOException {
        Student student = new Student();
        student.name="张三";
        student.age=18;
		//创建序列化对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
        //将student序列化到a.txt中
        oos.writeObject(student);
        oos.close();
    }
}

ObjectInputStream

构造方法

  • public ObjectInputStream(InputStream in) : 根据输入流创建反序列化对象

读取对象的方法

  • public final Object readObject () : 读取一个对象

例子

public class Main {
    public static void main(String[] args) throws Exception {
      //创建反序列化对象
      ObjectInputStream ois=new ObjectInputStream(new FileInputStream("a.txt"));
      //将a.txt中存储的对象反序列化到内存中
      Student student=(Student) ois.readObject();
      System.out.println(student.name);
      System.out.println(student.age);
      student.eat();
      //释放资源
      ois.close();
    }
}

注意事项

  在序列化对象后只要修改了文件则反序列化就会失败,就会抛出异常;即使文件的内容最后和修改之前一样也会抛异常,例如在文件中加了一个字符又删掉了,这种情况会抛出异常。


   Serializable 接口给需要序列化的类,提供了一个序列版本号serialVersionUID。该版本号的目的在于验证序列化的对象和对应类是否版本匹配。当序列化的类提供了一个序列版本号serialVersionUID,则只要修改之前和修改之后的内容一致则不会抛异常,否则会抛出异常

public class Student implements Serializable {

    private static final long serialVersionUID=1L;
    public String name;
    public transient int age;//被transient修饰不会被序列化

    public void eat(){
        System.out.println("开吃了");
    }
}

打印流

构造方法

  • public PrintStream(String fileName) :根据文件名创建打印流对象

例子

public class Main {
    public static void main(String[] args) throws Exception {
        PrintStream ps = new PrintStream("a.txt");
        //调用系统的打印流,输出到控制台上
        System.out.println("Hello");
        //设置系统的打印流的流向,让其输出到a.txt中
        System.setOut(ps);
        System.out.println("Java");
        ps.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值