IT科技

详解java中的缓冲流、转换流、序列化流

时间:2010-12-5 17:23:32  作者:应用开发   来源:应用开发  查看:  评论:0
内容摘要:一、缓冲流缓冲流的基本原理,是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO次数,从而提高读写的效率。字节缓冲流构造方法创建字节缓冲输入流:BufferedInpu

 一、详解序列缓冲流缓冲流的缓冲换流化流基本原理,是流转在创建流对象时,会创建一个内置的详解序列默认大小的缓冲区数组,通过缓冲区读写,缓冲换流化流减少系统IO次数,流转从而提高读写的详解序列效率。

字节缓冲流构造方法

创建字节缓冲输入流:

BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bis.txt")); 

创建字节缓冲输出流:

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt")); 

代码演示:

public class Demo {      public static void main(String[] args) throws IOException {         // 记录开始时间        long start = System.currentTimeMillis();        // 创建流对象        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("ChromeSetup.exe"));                 BufferedOutputStream bos = new BufferedOutputStream(                        new FileOutputStream("d:\\ChromeSetup_copy.exe"));) {              // 读写数据            int len;             byte[] bytes = new byte[8 * 1024];             while ((len = bis.read(bytes)) != -1) {                  bos.write(bytes,缓冲换流化流 0, len);             }        } catch (IOException e) {             e.printStackTrace();        }        // 记录结束时间        long end = System.currentTimeMillis();         System.out.println("缓冲流使用数组复制时间:" + (end - start) + " 毫秒");     }} 

输出结果为:

缓冲流使用数组复制时间:10 毫秒

字符缓冲流构造方法

创建字符缓冲输入流

BufferedReader br = new BufferedReader(new FileReader("br.txt")); 

创建字符缓冲输出流

BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt")); 

特有方法

BufferedReader: public String readLine() : 读一行文字。

BufferedWriter: public void newLine() : 写一行行分隔符,流转由系统属性定义符号。

readLine 方法演示public class Demo {      public static void main(String[] args) throws IOException {          // 创建流对象         BufferedReader br = new BufferedReader(new FileReader("a.txt"));         // 定义字符串,详解序列保存读取的一行文字         String line = null;         // 循环读取,读取到最后返回null         while ((line = br.readLine())!=null) {              System.out.println(line);         }         // 释放资源         br.close();          } } 

输出结果为:

aaaaa bbbbbccccc 

public class Demo {      public static void main(String[] args) throws IOException {          // 创建流对象         BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));         // 写出数据         bw.write("ccccc");         // 写出换行         bw.newLine();         bw.write("bbbbb");         bw.newLine();         bw.write("aaaaa");         bw.newLine();         // 释放资源         bw.close();     } } 

输出结果为:

二、转换流InputStreamReader类

转换流 java.io.InputStreamReader ,缓冲换流化流是流转Reader的子类,,读取字节并使用指定的详解序列字符集将其解码为字符。它的缓冲换流化流字符集可以自定义,也可以用平台的流转默认字符集。

构造方法

InputStreamReader(InputStream in) : 创建一个使用默认字符集的字符流。

InputStreamReader(InputStream in, String charsetName) : 创建一个指定字符集的字符流。

OutputStreamWriter类

转换流 java.io.OutputStreamWriter ,是Writer的子类,网站模板用指定的字符集将字符编码为字节。它的字符集可以自定义,也可以用平台的默认字符集。

构造方法

OutputStreamWriter(OutputStream in) : 创建一个使用默认字符集的字符流。

OutputStreamWriter(OutputStream in, String charsetName) : 创建一个指定字符集的字符流。

代码演示: public class Demo {      public static void main(String[] args) throws IOException {         // 1.定义文件路径         String a= "a.txt";         String b= "b.txt";         // 2.创建流对象         // 2.1 转换输入流,指定GBK编码         InputStreamReader isr = new InputStreamReader(new FileInputStream(a) , "GBK");         // 2.2 转换输出流,默认utf8编码         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(b));        // 3.读写数据         // 3.1 定义数组         char[] c = new char[1024];         // 3.2 定义长度         int len;         // 3.3 循环读取         while ((len = isr.read(c)) != -1) {              // 循环写出        osw.write(c,0,len);         }        // 4.释放资源         osw.close();         isr.close();     }} 

三、序列化流

ObjectOutputStream类

java.io.ObjectOutputStream 类,将Java对象的原始数据类型写出到文件,实现对象的持久存储。

构造方法

public ObjectOutputStream(OutputStream out) : 创建一个指定OutputStream的ObjectOutputStream。

对象序列化注意:

该类必须实现 java.io.Serializable 接口,Serializable是一个标记接口,不实现此接口的类将不会使任何状态序列化或反序列化,会抛出 NotSerializableException 。

该类的所有属性必须是可序列化的。如果有一个属性不需要可序列化的,则该属性必须注明是瞬态的,使用 transient 关键字修饰

写出对象方法:

public final void writeObject (Object obj) : 将指定的云南idc服务商对象写出

ObjectInputStream类

ObjectInputStream反序列化流,将之前使用ObjectOutputStream序列化的原始数据恢复为对象。

构造方法

public ObjectInputStream(InputStream in) : 创建一个指定InputStream的ObjectInputStream。

对象反序列化注意:

必须是能够找到class文件的类。如果找不到该类的class文件,则抛出一个 ClassNotFoundException 异常。

能找到class文件,但是class文件在序列化对象之后发生了修改,那么反序列化操作也会失败,抛出一个 InvalidClassException 异常

如果能找到一个对象的class文件,我们可以进行反序列化操作,调用 ObjectInputStream 读取对象的方法:

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

代码演示:

public class Demo {      public static void main(String[] args) throws Exception {         // 创建 学生对象        Student student = new Student("张三", "zahgnsan");         Student student2 = new Student("李四", "lisi");         Student student3 = new Student("王五", "wagnwu");         ArrayList<Student> arrayList = new ArrayList<>();        arrayList.add(student);        arrayList.add(student2);        arrayList.add(student3);        // 序列化操作        serializ(arrayList);        // 反序列化        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("list.txt"));         // 读取对象,强转为ArrayList类型                ArrayList<Student> list = (ArrayList<Student>) ois.readObject();        for (int i = 0; i < list.size(); i++) {              Student s = list.get(i);            System.out.println(s.getName() + "--" + s.getPwd());         }    }    private static void serializ(ArrayList<Student> arrayList) throws Exception {         // 创建 序列化流        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("list.txt"));         // 写出对象        oos.writeObject(arrayList);        // 释放资源        oos.close();     }} 

四、打印流

PrintStream类 java.io.PrintStream 类,该类能够方便地打印各种数据类型的值

构造方法

public PrintStream(String fileName) : 使用指定的文件名创建一个新的打印流

System.out 就是 PrintStream 类型的,只不过它的流向是系统规定的,打印在控制台上

代码演示:

public class Demo {       public static void main(String[] args) throws IOException {           // 控制台直接输出张三          System.out.println("张三");          // 创建打印流,指定文件的名称          PrintStream zs = new PrintStream("zs.txt");          // 设置系统的源码库打印流流向,输出到zs.txt          System.setOut(zs);          // 调用系统的打印流,zs.txt中输出张三          System.out.println("张三");      }  }  

【编辑推荐】

如何在 Linux 终端中连接 WiFi? 分析Netflix公司产品Spinnaker的微服务实践 如何在Docker容器中运行Docker 「3种方法」 “禁令”周日在美生效:TikTok坚持诉讼,企业微信海外版悄悄改名 使用开源工具 nccm 管理 SSH 连接  
copyright © 2025 powered by 益强资讯全景  滇ICP备2023006006号-31sitemap