java的 I/O 输入输出流详解

本文详细介绍了Java中的I/O输入输出流,包括流的定义、四大基本流、节点流与处理流的区别、以及缓冲流、数据流、转换流和对象序列化的应用。通过实例代码展示了如何使用FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream等进行文件读写,强调了缓冲流在提高效率方面的重要性,并探讨了字节流与字符流在处理不同格式文件时的区别。此外,还讲解了Print流、Data流、Object流以及如何实现对象的序列化和反序列化。
摘要由CSDN通过智能技术生成

I/O 输入输出流

流的定义:

流就是程序和设备之间嫁接起来的一根用于数据传输的管道,这个管道上有很多按钮,不同的按钮可以实现不同的功能。

这根用于数据传输的管道就是流,流就是一根管道

流一定是类,但类不一定是流;流是抽象类不能new对象

输入时,程序在源(文件,网络,内存)上打开一个流,然后如图一个一个顺序读。写也一样。

流的分类和使用:

四大基本抽象流,文件流,缓冲流,转换流,数据流,Print流,Object流。

JAVA.io 包中定义了多个流类型(类或抽象类)来实现输入/输出功能;可以从不同角度对其进行分类:

*按数据流的方向不用可以分为输入流和输出流

*按处理数据单位不同可以分为字节流和字符流

*按照功能不同可以分为节点流和处理流

JAVA中所提供的的所有流类型位于包JAVA.io内,都分别继承自以下四种抽象流类型:

节点流与处理流:

节点流可以从一个特定的数据源(节点)读取数据(如:文件,内存)

处理流是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能。

节点流也叫原始流,处理流也叫包裹流。

 

流与类的关系:

如果一个类是用作设备和程序之间的数据传输,则这个类有一个新的名字叫做流

流一定是类,但类不一定是流

 

四大基本流的介绍

输入流,输出流,字节流,字符流

InputStream和OutputStream读写数据的单位是一个字节

Reader和Writer读写数据的单位是一个字符

在JAVA中一个字符占两个字节

InputStream,OutputStream,Reader,Writer都是抽象类,或者说都是抽象流,通常我们使用的都是它们的子类,凡是以Stream结尾的都是字节流。

 

InputStream 流中的常用方法:

 

 

OutputStream 流中的常用方法:

 

 

Reader 流中的常用方法:

 

 Writer 流中的常用方法:

 

 

文件流

 文件流包括:

FileInputStream FileOutputStream --字节流

FileReader FileWriter --字符流

实例:读取一个文件的内容并将其输出到显示器上,并统计读取的字节个数

/*

    利用FileReader流来读取一个文件中的数据,并在显示器上输出!

*/

 

import java.io.*;

 

public class TestFileReader {

    public static void main(String[] args) {

        FileReader fr = null;

 

        try {

            fr = new FileReader("C:\\Documents and Settings\\others\\桌面\\java\\TestFileReader.java");

            int cnt = 0;

            int ch;

 

            while (-1 != (ch = fr.read())) // 20行

            {

                System.out.print((char) ch); // System.out.print(int ch);

                                                // 这是在显示器上输出ch的整数值,所以必须的进行类型转化,我们需要输出的是ch所代表的整数对应的字符

                ++cnt;

            }

 

            System.out.printf("总共从TestFileReader.java文件中读取了%d个字符", cnt);

        } catch (FileNotFoundException e) {

            System.out.println("找不到文件!");

            System.exit(-1);

        } catch (IOException e) {

            System.out.println("文件读取失败!");

            System.exit(-1);

        }

    }

}

 

FileInputStream的使用

 

FileReader的使用

字节流与字符流的区别:

FileInputStream 和FileOutputStream 可以完成所有格式文件的复制

FileReader和FileWriter只可以完成文本文件的复制,却无法完成其他格式文件的复制

因为字节是不需要解码和编码的,将字节转化为字符才存在解码和编码的问题

字节流可以从所有格式的设备中读写数据,但字符流只能从文本格式的设备中读写数据

/*

    利用FileInputStream 和 FileOutputStream 可以完成所有格式文件的赋值

    因为字节是不需要解码和编码的,将字节转化为字符才存在解码的问题

    本程序完成了音频文件的复制

*/

 

import java.io.*;

 

public class TestFileInputStreamOutputStreamCopy {

    public static void main(String[] args) {

        FileInputStream fi = null;

        FileOutputStream fo = null;

 

        try {

            fi = new FileInputStream("E:\\综艺\\歌曲\\卡农.mp3");

            fo = new FileOutputStream("d:/share/Output.txt"); //使用播放器可正常播放该文件

            int ch;

 

            while (-1 != (ch = fi.read())) {

                fo.write(ch);

            }

        } catch (FileNotFoundException e) {

            System.out.println("文件没有找到!");

            System.exit(-1);

        } catch (IOException e) {

            System.out.println("文件读写错误!");

            System.exit(-1);

        } finally {

            try {

                if (null != fi) {

                    fi.close();

                    fi = null;

                }

                if (null != fo) {

                    fo.close();

                    fo = null;

                }

            } catch (Exception e) {

                e.printStackTrace();

                System.exit(-1);

            }

        }

 

        System.out.println("文件复制成功!");

    }

}

 

 

/*

    本程序证明了 FileReader 和 FileWriter 只可以完成文本文件的复制,

            却无法完成音频格式文件的复制

*/

 

import java.io.*;

 

public class TestFileReaderWriterCopy {

    public static void main(String[] args) {

        FileReader fi = null;

        FileWriter fo = null;

 

        try {

            fi = new FileReader("E:\\综艺\\歌曲\\卡农.mp3");

            fo = new FileWriter("d:/share/Output.txt"); // Output.txt使用播放器打开失败!

                                                        // 本程序证明了FileWriter 和

                                                        // FileReader

                                                        // 无法完成音频文件的复制,实际上FileWriter

                                                        // 和 FileReader

                                                        // 只能完成文本文件的复制

            int ch;

 

            while (-1 != (ch = fi.read())) {

                fo.write(ch);

            }

        } catch (FileNotFoundException e) {

            System.out.println("文件没有找到!");

            System.exit(-1);

        } catch (IOException e) {

            System.out.println("文件读写错误!");

            System.exit(-1);

        } finally {

            try {

                if (null != fi) {

                    fi.close();

                    fi = null;

                }

                if (null != fo) {

                    fo.close();

                    fo = null;

                }

            } catch (Exception e) {

                e.printStackTrace();

                System.exit(-1);

            }

        }

 

        System.out.println("文件复制成功!");

    }

}

缓冲流

缓冲流就是带有缓冲区的输入输出流

缓冲流可以显著的减少我们对IO访问的次数,保护我们的硬盘

缓冲流本事就是处理流(包裹流),缓冲流必须得依附于节点流(原始流)

处理流包裹在原始节点流上的流,相当于包裹在管道上的管道

 

缓冲流要"套接"在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法。JAVA提供了四种缓冲流,其常用的构造方法为:

 

 

BufferedOutputStream 和 BufferedInputStream

BufferedOutputStream :带有缓冲的输出流,允许一次向硬盘写入多个字节的数据。

BufferedInputStream:带缓冲的输入流,允许一次向程序中读入多个字节的数据。

BufferedOutputStream 和 BufferedInputStream都是包裹流,必须依附于OutputStream和InputStream

例子:利用BufferedOutputStream 和 BufferedInputStream 完成大容量文件的复制,这远比单纯利用FileInputStream和FileOutputStream要快的多

/*

    利用BufferedOutputStream 和 BufferedInputStream完成大容量文件的复制

    这远比单纯利用 FileInputStream  和 FileOutputStream 要快得多

 

    BufferedOutputStream 和 BufferedInputStream 都是包裹流,必须的依附于

    InputStream 和 OutputStream

*/

 

import java.io.*;

 

public class TestBufferedInputStreamOutputStreamCopy {

    public static void main(String[] args) {

        BufferedOutputStream bos = null;

        BufferedInputStream bis = null;

 

        try {

            bos = new BufferedOutputStream(new FileOutputStream("e:/OutputView.txt")); // bos

                                                                                        // 输出流有个默认的缓冲区,大小为32个字节

 

            bis = new BufferedInputStream(new FileInputStream("c:\\[高清在线www.66ys.cn]海底总动员DVD中英字幕.rmvb")); // bis

                                                                                                            // 输入流有个默认的缓冲区,大小为32个字节

            byte[] buf = new byte[1024];

            int len = bis.read(buf, 0, 1024); // 一定要注意,这不是从buf中读数据,而是从bis所关联到的D:\\综艺\\电影\\猫和老鼠\\CD4.rmvb文件中读取数据,并将读取的数据写入bis自己的默认缓冲区中,然后再将缓冲区的内容写入buf数组中,每次最多向buf数组中写入1024个字节,返回实际写入buf数组的字节个数,如果读到了文件的末尾,无法再向buf数组中写入数据,则返回-1

            while (-1 != len) {

                bos.write(buf, 0, len); // 不是写入buf数组,而是将buf数组中下标从0开始的到len-1为止的所有数据写入bos所关联到的"d:/share/OutputView.txt"文件中

                len = bis.read(buf); // bis.read(buf); 等价于 bis.read(buf, 0,

                                        // buf.length);

            }

            bos.flush();

            bis.close();

            bos.close();

        } catch (FileNotFoundException e) {

            System.out.println("没有找到文件!");

            System.exit(-1);

        } catch (IOException e) {

            System.out.println("文件读写错误!");

            System.exit(-1);

        }

 

        System.out.println("文件复制成功!");

    }

}

 

 /*

    本程序读写速度要慢于  "TestBufferedInputStreamOutputStreamCopy.java" 程序

    即:

    利用BufferedOutputStream 和 BufferedInputStream完成大容量文件的复制

    这远比单纯利用 FileInputStream  和 FileOutputStream 要快得多

 

    BufferedOutputStream 和 BufferedInputStream 都是包裹流,必须的依附于

    OutputStream  和 OutputStream

*/

 

import java.io.*;

 

public class TestBufferedInputStreamOutputStreamCopy_2 {

    public static void main(String[] args) {

        FileOutputStream bos = null;

        FileInputStream bis = null;

 

        try {

            bos = new FileOutputStream("e:/OutputView.txt");

            bis = new FileInputStream("c:\\[高清在线www.66ys.cn]海底总动员DVD中英字幕.rmvb");

 

            byte[] buf = new byte[1024];

            int len = bis.read(buf, 0, 1024);

            while (-1 != len) {

                bos.write(buf, 0, len);

                len = bis.read(buf);

            }

            bos.flush();

            bis.close();

            bos.close();

        } catch (FileNotFoundException e) {

            System.out.println("没有找到文件!");

            System.exit(-1);

        } catch (IOException e) {

            System.out.println("文件读写错误!");

            System.exit(-1);

        }

 

        System.out.println("文件复制成功!");

    }

}

 

 

一定要注意,bis.read(buf,0,1024);这不是从buf中读数据,而是从bis所关联到的“D:\\综艺\\电影\\猫和老鼠\\CD4.rmvb”文件中读取数据,并将读取的数据写入bis自己的默认缓冲区中,然后再将缓冲区的内容写入buf数组中,每次最多向buf数组中写入1024个字节,返回实际写入buf数组的字节个数,如果读到了文件的末尾,无法再向buf数组中写入数据,则返回-1

BufferedInputStream流中有public int read(byte[] b)方法用来把从当前流关联到的设备中读取出来的数据存入一个byte数组中

BufferedOutputStream 流中有public int write(byte[] b)方法用来把byte数组中的数据输出来当前流所关联到的设备中

如果我们希望用BufferedInputStream 和 BufferedOutputStream 完成“将一个设备中的数据导入另一个设备中”,我们就应该定义一个临时的byte类型的数据,用这个临时数组作为输入流和输出流进行交互的中转枢纽。

实例:利用BufferedReader 和 BufferedWriter完成文本文件的复制

/*

    利用 BufferedReader 和 BufferedWriter 完成文本文件的复制

*/

import java.io.*;

 

public class TestBufferedReaderWriterCopy {

    public static void main(String[] args) {

        BufferedReader br = null;

        BufferedWriter bw = null;

 

        try {

            br = new BufferedReader(

                    new FileReader("C:\\Documents and Settings\\others\\桌面\\java\\TestBufferedReaderWriterCopy.java"));

            bw = new BufferedWriter(new FileWriter("d:/share/Writer.txt"));

            String str = null;

 

            while (null != (str = br.readLine())) // br.readLine()读取一行字符,但会将读取的换行符自动丢弃,即返回的String对象中并不包括换行符

            {

                bw.write(str);

                bw.newLine(); // 写入一个换行符 这行不能省

            }

            bw.flush();

        } catch (FileNotFoundException e) {

            e.printStackTrace();

            System.exit(-1);

        } catch (IOException e) {

            e.printStackTrace();

            System.exit(-1);

        } finally {

            try {

                bw.close();

                br.close();

            } catch (IOException e) {

                e.printStackTrace();

                System.exit(-1);

            }

        }

    }

}

 

 数据流DataInputStream DataOutputStream

DataInputStream能够以一种与机器无关的方式,直接从底层字节输入流读取JAVA基本类型和String类型的数据。常用方法包括:

DataInputStream 是包裹流,必须依附于InputStream

DataOutputStream能够以一种机器无关的方式,直接将JAVA基本类型和String类型数据写出到其他的字节输出流。常用方法包括:

DataOutputStream 是包裹流,它必须依附于OutputStream

数据流实例:

编程实现将long类型数据写入byte数组,然后再从byte数组中吧该数据读出来{

*这是Socket编程中经常要完成的功能。

*因为网络编程中经常要把数据存入byte数组中,然后把byte数组打包成数据包(DatagramPacket),再把数据包经过网络传输到目的机,目的机再从byte数组中把原数值型数据还原回来。

}

本程序要使用到:

DataInputStream 

DataOutputStream

ByteArrayInputStream

ByteArrayOutputStream

/*

    功能:把一个long类型的数据写入byte数组中,然后再从byte数组中读取出

          这个long类型的数据

          因为网络编程中经常要把数值型数据存入byte数组中然后打包成

          DatagramPacket经过网络传输到目的机,目的机再从byte数组中

          把原数值型数据还原回来

    目的: ByteArrayOutputStream   DataOutputStream  ByteInputStream DataInputStream 流的使用

          记住: DataOutputStream流中的writeLong(long n)是把n变量在内存

                  中的二进制代码写入该流所连接到的设备中

                  

    注意:查API文档得知:

            构造  ByteArrayOutputStream 对象时不需要也不能指定缓冲数组,因为缓冲数组默认已经内置好了         

              构造  ByteArrayInputStream 对象时必须的指定缓冲数组是谁!

*/

 

import java.io.*;

 

public class TestByteArrayOutputStream1

{

    public static void main(String args[]) throws Exception

    {

        long n = 9876543210L;

        ByteArrayOutputStream baos = new ByteArrayOutputStream();  //9行  API:"public ByteArrayOutputStream(): 创建一个新的 byte 数组输出流。缓冲区的容量最初是 32 字节,如有必要可增加其大小。 "

                                    //9行代码一旦执行完毕,意味着两点: 1、在内存中生成了一个大小为32个字节的byte数组   2、有一根叫做baos的管道已链接到了该byte数组中,并且可以通过这个管道向该byte数组中写入数据

                                    //虽然此时可以通过baos向baos所连接到的在内存中分配好的byte数组中写入数据,但是ByteArrayOutputStream流并没有提供可以直接把long类型数据直接写入ByteArrayOutputStream流所连接到的byte数组中的方法, 简单说我们没法通过baos向baos所连接到的byte数组中写入long类型的数据, 查API文档可以发现: ByteArrayOutputStream流中并没有类似writeLong()这样的方法,但是DataOutputStream流中却有writeLong() writeFloat()等方法

        DataOutputStream dos = new DataOutputStream(baos);

        dos.writeLong(n);  //把n变量所代表的10000L在内存中的二进制代码写入dos所依附的baos管道所连接到的内存中的大小为32字节的byte数组中,由运行结果来看,这是二进制写入,既不是把10000L转化为字符'1' '0' '0' '0' '0'写入byte数组中,而是把10000L在内存中的总共8个字节的二进制代码写入byte数组中 

        dos.flush();

        byte[] buf = baos.toByteArray();  //DataOutputStream 流中并没有toByteArray()方法,但是ByteArrayOutputStream 流中却有toByteArray()方法, 所以不可以把baos 改为dos,否则编译时会出错! ByteArrayOutputStream流中toByteArray()方法的含义,摘自API“创建一个新分配的 byte 数组。其大小是此输出流的当前大小,并且缓冲区的有效内容已复制到该数组中”

        //利用ByteArrayInputStream 和 DataInputStream 可以从byte数组中得到原long类型的数值10000L

        ByteArrayInputStream bais = new ByteArrayInputStream(buf);

        DataInputStream dis = new DataInputStream(bais);

        long l = dis.readLong();

        System.out.println("l = " + l);

        dos.close();

    }

}

/*

    在JDK 1.6中的运行结果是:

----------------

l = 9876543210

----------------

*/

 /*

    功能:

        将long类型数据写入byte数组,然后在从byte数组中把该数据读出来

*/

import java.io.*;

public class TestByteArrayOutputStream2

{

    public static void main(String[] args) throws Exception

    {

        long n = 1234567;

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        DataOutputStream dos = new DataOutputStream(baos);

        dos.writeLong(n);

        

        byte[] buf = baos.toByteArray();

        ByteArrayInputStream bis = new ByteArrayInputStream(buf);

        DataInputStream dis = new DataInputStream(bis);

        long n2 = dis.readLong();

        System.out.println("n2 = " + n2);

        

        dos.close();

        dis.close();

    }

}

 

 转换流:OutputStreamWriter InputStreamReader

OutputStreamWriter 流是把OutputStream流 转化成Writer流的流

InputStreamReader 流是把InputStream流转化为Reader

OutputStreamWriter 和 InputStreamReader都是包裹流

实例:如何将键盘输入的字符组成字符串直接赋给String对象。

/*

    如何将键盘输入的字符组成字符串直接赋给String 对象

*/

import java.io.*;

public class TestStringInput

{

    public static void main(String[] args)

    {

        String str = null;

        BufferedReader br = new BufferedReader (   //21行

                    new InputStreamReader(System.in)

                ); //23行  查API:从21行到23行的代码是不会抛出任何异常的

                

        try

        {

            str = br.readLine(); //会抛出IOException异常

        }

        catch (IOException e)

        {

            e.printStackTrace();

            System.exit(-1);

        }   

        System.out.println("str = " + str);        

        try

        {

            br.close(); //会抛出IOException异常

        }

        catch (IOException e)

        {

            e.printStackTrace();

            System.exit(-1);

        }        

    }

}

/*

    在JDK 1.6中的运行结果是:

--------------------------------

sadd行政村123Asd?asd撒旦

str = sadd行政村123Asd?asd撒旦

--------------------------------

*/

 readLine()与回车符的问题:

 

Print流  PrintWriter PrintStream

Print 流只有输出,没有输入

分类:

PrintWriter输入字符

PrintStream输出字符

PrintWriter在OutputStream基础之上提供了增强的功能,既可以方便地输出各种类型数据(而不仅限于byte型)的格式化表示形式。

PrintStream重载了print和println方法,用于各种不同类型数据的格式化输出。

格式化输出是指将一个数据用其字符串格式输出。

 

DataOutputStream 中的 WriteXXX(data)方法是把data在内存中的二进制数据写入文件

PrintStream 中的println(data)是该数据格式化后的字符串写入文件

/*

    DataOutputStream 中的 writeXXX(data)方法与PrintStream 中的 println(data)的区别

    总结:

        DataOutputStream 中的 writeXXX(data)方法是把data在内存中的二进制数据写入文件

        PrintStream 中的 println(data)写出的是该数据的格式化后的字符串        

*/

import java.io.*;

public class TestPrintStream_1

{

    public static void main(String[] args) throws Exception

    {

        DataOutputStream dos = new DataOutputStream(new FileOutputStream("d:/share/kk.txt"));

        dos.writeLong(12345);  //实际写入文件的是00 00 00 00 00 00 30 39

        dos.close();

        System.out.printf("%#X\n", 12345);

        PrintStream ps = new PrintStream(new FileOutputStream("d:/share/kk2.txt"), true);

        ps.println(12345);  //实际写入文件的是'1' '2' '3' '4' '5'

        ps.close();

    }

}

 PrintWriter 提供了PrintStream的所有打印方法, 其方法也从不抛出IOException。

与PrintStream的区别:

标准输入输出的重定向:

实例:编程实现将键盘输入的数据输入A文件中,如果输入有误,则把出错信息输出到B文件

import java.io.*;

public class TestSetSystemOut {

    public static void main(String[] args) {

        PrintStream ps_out = null;

        try {

            ps_out = new PrintStream(new FileOutputStream("d:/share/ww.txt"));

            System.setOut(ps_out); // 将System.out的值重新设置为ps_out,即System.out不在关联到显示器,而是关联到"d:/share/ww.txt"文件

            System.out.println(12); // 这实际上是把12输出到了System.out所关联的d:/share/ww.txt中

            System.out.println(55.5); // 同上

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            try {

                ps_out.close();

            } catch (Exception e) {

                e.printStackTrace();

            }

 

        }

    }

}

 

 /*

    功能: 将键盘输入的数据输入A文件中,如果输入有误,

           则把出错信息输出到B文件中

           

           标准输入输出流的重定向    

*/

 

import java.io.*;

import java.util.*;

 

public class TestSetOutErr {

    public static void main(String[] args) {

        PrintStream psOut = null;

        PrintStream psError = null;

        Scanner sc = null;

 

        try {

            psOut = new PrintStream("d:/Out.txt");

            psError = new PrintStream("d:/error.txt");

            sc = new Scanner(System.in);

            int num;

            System.setOut(psOut);

            System.setErr(psError);

 

            while (true) {

                num = sc.nextInt();

                System.out.println(num);

            }

        } catch (Exception e) {

            System.err.println("出错的信息是:"); // 不可以写成System.out.println("出错的信息是:");

            e.printStackTrace(); // e.printStackTrace(); 默认是输出到System.err所关联的设备中

        }

    }

}

 

 对象的序列化 ObjectOutputStream ObjectInputStream

所谓序列化是指:把一个Object对象直接转化为字节流,然后把这个字节流直接写入本地硬盘或网络中

序列化流(ObjectOutputStream)----writeObject

反序列化流(ObjectInputStream)---readObject

如果想把某个对象序列化,则必须实现Serializable接口

transient关键字用用它修饰变量后该变量不会进行jvm默认的序列化

当我们要把一个对象在网络上传输转化成字节序列,我们有些变量没必要使用,放在网络上传输会浪费资源,这个时候我们就会用transient关键字

transient修饰的属性我们可以自己完成序列化

transient关键字在有些情况下可以提高性能

实例:

import java.io.*;

 

public class TestObjectIO

{

    public static void main(String[] args)

    {

        ObjectOutputStream oos = null;

        ObjectInputStream ois = null;

        Student ss = new Student("zhansan", 1000, 88.8f);  //注意88.8f不能改为88.8

        Student ss2 = null;    

                

        try

        {

            FileOutputStream fos = new FileOutputStream("d:/share/java/ObjectOut.txt");

            oos = new ObjectOutputStream(fos);

            oos.writeObject(ss);

            

            ois = new ObjectInputStream(new FileInputStream("d:/share/java/ObjectOut.txt"));    

            ss2 = (Student)ois.readObject();  //(Student)不能省   ois.readObject();如果ois中的某个成员是transient,则该成员是不会被读取的,因为该成员不会被保存,何来读取之说?!

            

            System.out.println("ss2.sname = " + ss2.sname);

            System.out.println("ss2.sid = " + ss2.sid);

            System.out.println("ss2.sscore = " + ss2.sscore);

        }

        catch (FileNotFoundException e)

        {

            System.out.println("文件没有找到!");

            System.exit(-1);

        }

        catch (Exception e)

        {

            e.printStackTrace();

            System.exit(-1);

        }

        finally

        {

            try

            {

                oos.close();

                ois.close();

            }

            catch (Exception e)

            {

                e.printStackTrace();

                System.exit(-1);

            }

        }        

    }

}

class Student implements Serializable  //如果将implements Serializable 注释掉,则程序编译时就会报错

{

    public String sname = null;

    public int sid = 0;

    transient public float sscore = 0; //表示sscore成员不能被序列化,所谓不能被序列化就是指:“该成员调用ObjectOutputStream 的writeOnbject()时不会被保存,调用ObjectInputStream的readObject()方法时不会被读取”

    

    public Student(String name, int id, float score)

    {

        this.sname = name;

        this.sid = id;

        this.sscore =  score;

    }

}

 分析ArrayList源码中序列化和反序列化的问题

ArrayList的底层虽然是一个数组,但是这个数组不一定放满,没有放满的数组元素是不需要进行序列化的,我们必须自己完成序列化,把有效元素一个一个自己完成序列化 ,反序列化也一样,所以ArrayList源码中的序列化和反序列化的作用就是把ArrayList中的有效元素进行序列化 无效元素不进行序列化,可以提高性能。

对ArrayList的源码有个初步的了解,能够进行序列化的优化问题

序列化中 子类和父类构造函数的调用问题

当一个子类的父类实现了序列化接口,子类可以直接进行序列化 ,子类序列化时会递归调用父类的构造方法。

对子类对象进行反序列化操作时,如果其父类没有实现序列化接口,那么其父类的构造方法会被调用

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值