(21) java IO 学习笔记

(1)文件的编码

GBK        一个汉字 两个字节  一个英文字符 一个字节         慕(c4 bd)课(bf ce)A(41)B(42)C(43)

UTF-8     一个汉字 三个字节  一个英文字符 一个字节          慕(e6 85 95)课(e8 af be)A(41)B(42)C(43)

utf-16be  一个汉字 两个字节  一个英文字符 两个字节         慕(61 55)课(8b fe)A(0 41)B(0 42)C(0 43)


乱码出现的原因:

当你的字节序列是某一种编码格式时,此时想把字节序列变为字符串 也需要使用这种编码格式,否则将出现乱码。 

//项目默认编码为GBK
public class EncodeDemo {
	public static void main(String[] args) throws UnsupportedEncodingException {
		String s = "慕课ABC";
		// 1////////////////////////////////////
		byte[] bytes1 = s.getBytes(); //没有指定具体的编码,则以项目当前默认的编码格式GBK
		for (byte b : bytes1) {
			// 把字节(转换成int 前面加了24个0 1个字节转成了4个字节 &0xff 去掉24个0) 以16进制的方式显示
			// 说明 GBK 一个汉字两个字节 一个英文字符一个字节   慕(c4 bd)课(bf ce)A(41)B(42)C(43)
			System.out.print(Integer.toHexString(b & 0xff) + " ");
		}
		System.out.println();
		// 2////////////////////////////////////
		byte[] bytes2 = s.getBytes("GBK"); // 显示指定编码格式
		for (byte b : bytes2) {
			// 输出结果仍为 :c4 bd bf ce 41 42 43
			// 说明 GBK 一个汉字两个字节 一个英文字符一个字节   慕(c4 bd)课(bf ce)A(41)B(42)C(43)
			System.out.print(Integer.toHexString(b & 0xff) + " ");
		}
		System.out.println();
		// 3////////////////////////////////////
		byte[] bytes3 = s.getBytes("UTF-8"); // 显示指定编码格式
		for (byte b : bytes3) {
			// 输出结果为 : e6 85 95 e8 af be 41 42 43
			// 说明 UTF-8 一个汉字三个字节 一个英文字符一个字节 慕(e6 85 95)课(e8 af be)A(41)B(42)C(43)
			System.out.print(Integer.toHexString(b & 0xff) + " ");
		}
		System.out.println();
		// 4////////////////////////////////////
		// java是双字节编码utf-16be
		byte[] bytes4 = s.getBytes("utf-16be"); // 显示指定编码格式
		for (byte b : bytes4) {
			// 输出结果为 : 61 55 8b fe 0 41 0 42 0 43
			// 说明 utf-16be 一个汉字 两个字节 一个英文字符 两个字节 慕(61 55)课(8b fe)A(0 41)B(0 42)C(0 43)
			System.out.print(Integer.toHexString(b & 0xff) + " ");
		}
		System.out.println();
		/*
		 * 当你的字节序列是某一种编码格式时,此时想把字节序列变为字符串也需要使用这种编码格式,否则将出现乱码。
		 */
		//字节数组转成 字符串
		String s1 = new String(bytes1);
		String s2 = new String(bytes2);
		System.out.println(s1);
		System.out.println(s2);
		//bytes3是通过s.getBytes("UTF-8")得到,new String(bytes3)是使用默认的GBK作为编码,故导致乱码
		String s3 = new String(bytes3);
		// 鎱曡ABC
		System.out.println(s3);
		// 解决乱码:
		String s32 = new String(bytes3, "UTF-8");
		System.out.println(s32);
		/*
		 * 文本文件 就是 字节序列 可以是 任意编码的 字节序列 如果我们在中文机器直接创建文本文件,那么该文本文件只认识ANSI编码
		 */
	}
}
结果:

c4 bd bf ce 41 42 43 
c4 bd bf ce 41 42 43 
e6 85 95 e8 af be 41 42 43 
61 55 8b fe 0 41 0 42 0 43 
慕课ABC
慕课ABC
鎱曡ABC
慕课ABC
=================================================================================
(2) JAVA.IO.FILE

java.io.File类用于表示文件(目录)。File类只用于表示文件(目录)的信息(名称、大小等),不能用于文件内容的访问。

2.1、File类基本API

public class FileDemo {
	public static void main(String[] args) {
		// 了解File类构造函数的情况 查帮助
		File file = new File("E:\\javaio\\imooc");
		// System.out.println(file.exists());  //file.exists() 测试此抽象路径名表示的文件或目录是否存在。
		if (!file.exists())
			file.mkdirs(); // file.mkdirs() 创建多级目录     file.mkdir() 在javaio目录存在的前提下创建imooc目录
		else
			file.delete();// 删除此抽象路径名表示的文件或目录。
		// 是否是一个目录 如果是目录返回true,如果不是目录or目录不存在返回的是false
		System.out.println(file.isDirectory());
		// 是否是一个文件
		System.out.println(file.isFile());

		// File file2 = new File("e:\\javaio\\日记1.txt");  //  \\   /  File.separator 
		File file2 = new File("e:\\javaio", "日记1.txt");
		if (!file2.exists())
			try {
				file2.createNewFile();//当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		else
			file2.delete();
		// 常用的File对象的API
		/*
		E:\javaio\imooc
		E:\javaio\imooc
		imooc
		日记1.txt
		E:\javaio
		e:\javaio
		E:\javaio
		*/
		System.out.println(file);// file.toString()的内容
		System.out.println(file.getAbsolutePath());
		System.out.println(file.getName());
		System.out.println(file2.getName());
		System.out.println(file.getParent());
		System.out.println(file2.getParent());
		System.out.println(file.getParentFile().getAbsolutePath());
	}
}
2.2、遍历目录《递归的思想
package com.imooc.io;

import java.io.File;
import java.io.IOException;

public class FileUtilspjm2 {

	public static void listDirectory(File dir) throws IOException {

		// 首先校验目录存不存在
		if (!dir.exists()) { // 注意 :dir.exists()表示 文件或目录 是否存在。
			throw new IllegalArgumentException("目录" + dir + "不存在!");
		}
		// 再校验dir是否是目录
		if (!dir.isDirectory()) {
			throw new IllegalArgumentException(dir + "不是目录!");
		}

		// 1 String[] filenames = dir.list();
		/*
		 * //dir.list()返回的是 字符串数组 是文件或者目录的 名字 String[] filenames = dir.list();
		 * for (String string : filenames) { //打印的仅仅 只是 对应目录 下 目录或者是 文件的名字。
		 * 并不包含子目录的。 System.out.println(dir.getAbsolutePath()+"\\"+string); }
		 * 
		 * E:\星际争霸2\StarCraft II\.agent.db E:\星际争霸2\StarCraft II\.patch.result
		 * E:\星际争霸2\StarCraft II\Battle.net E:\星际争霸2\StarCraft II\Campaigns
		 * E:\星际争霸2\StarCraft II\Launcher.db E:\星际争霸2\StarCraft II\Logs
		 * E:\星际争霸2\StarCraft II\Mods E:\星际争霸2\StarCraft II\SetupWin.mpq
		 * E:\星际争霸2\StarCraft II\StarCraft II Editor.exe E:\星际争霸2\StarCraft
		 * II\StarCraft II.exe E:\星际争霸2\StarCraft II\StarCraft II.mfil
		 * E:\星际争霸2\StarCraft II\StarCraft II.tfil E:\星际争霸2\StarCraft II\Support
		 * E:\星际争霸2\StarCraft II\Updates E:\星际争霸2\StarCraft II\Versions
		 */

		// 2 listFiles()返回的是 FIle数组 是文件或者目录的 File对象。 对其子目录的遍历 需要用到递归的思想
		File[] filenames = dir.listFiles();
		// 首先对 filenames 进行校验,满足条件再进行目录遍历。
		if (filenames != null && filenames.length > 0) {
			for (File file : filenames) {
				if (file.isDirectory()) { //如果是个目录 则递归  继续遍历其子目录
					listDirectory(file);
				} else {                  //否则打印输出
					System.out.println(file);
				}
			}
		}
	}
	public static void main(String[] args) throws IOException {
		FileUtilspjm2.listDirectory(new File("E:\\星际争霸2\\StarCraft II"));
	}
}


(3)RandomAccessFile类

RandomAccessFile java提供的对文件内容的访问,既可以读文件,也可以写文件。
RandomAccessFile支持随机访问文件,可以访问文件的任意位置


(3.1)java文件模型
  在硬盘上的文件是byte byte byte存储的,是数据的集合
(3.2)RandomAccessFile打开文件   有两种模式"rw"(读写)  "r"(只读)
  RandomAccessFile raf = new RandomeAccessFile(file,"rw") 文件指针,  打开文件时指针在开头 pointer = 0;
(3.3) 写方法
    raf.write(int)--->只写一个字节(后8位),同时指针指向下一个位置,准备再次写入
(3.4)读方法
   int b = raf.read()--->读一个字节
(3.5)文件读写完成以后一定要关闭(Oracle官方说明)

public class RafDemopjm3 {

	public static void main(String[] args) throws IOException {
		File demo = new File("demo"); // 没指定具体路径 就是 相对路径 即项目路径下
		if (!demo.exists())
			demo.mkdir();
		File file = new File(demo, "raf.dat");
		if (!file.exists())
			file.createNewFile();

		RandomAccessFile raf = new RandomAccessFile(file, "rw");
		// 指针的位置
		System.out.println(raf.getFilePointer());

		raf.write('A');// 只写了一个字节 < char是两个字节的 >
		System.out.println(raf.getFilePointer());
		raf.write('B');// 只写了一个字节 < char是两个字节的 >

		int i = 0x7fffffff;
		// 用write方法每次只能写一个字节,如果要把i写进去就得写4次
		raf.write(i >>> 24);// 高8位
		raf.write(i >>> 16);
		raf.write(i >>> 8);
		raf.write(i);
		System.out.println(raf.getFilePointer());

		// 可以直接写一个int
		raf.writeInt(i);

		String s = "中";
		byte[] gbk = s.getBytes("gbk");
		raf.write(gbk);
		System.out.println(raf.length());

		// 读文件,必须把指针移到头部
		raf.seek(0);
		// 一次性读取,把文件中的内容都读到字节数组中
		byte[] buf = new byte[(int) raf.length()];
		raf.read(buf);

		System.out.println(Arrays.toString(buf));
		for (byte b : buf) {
			System.out.print(Integer.toHexString(b & 0xff) + " ");
		}
		raf.close();
	}
}
结果:

0
1
6
12
[65, 66, 127, -1, -1, -1, 127, -1, -1, -1, -42, -48]
41 42 7f ff ff ff 7f ff ff ff d6 d0 

(4)字节流 (IO 流)

1.字节流
 1) InputStream、OutputStream
    InputStream抽象了应用程序读取数据的方式
    OutputStream抽象了应用程序写出数据的方式
 
 2)EOF = End   读到-1就读到结尾
 3)输入流基本方法
    int  b = in.read();读取一个字节无符号填充到int低八位.-1是 EOF
    int  b = in.read(byte[] buf) 
    int  b = in.read(byte[] buf,int start,int size)
4)输出流基本方法
  out.write(int b)  写出一个byte到流,b的低8位
  out.write(byte[] buf)将buf字节数组都写入到流
  out.write(byte[] buf,int start,int size)
  
 5)FileInputStream    --->具体实现了在文件上读取数据
 6)FileOutputStream --->实现了向文件中写出byte数据的方法
 7)DataOutputStream/DataInputStream  对"流"功能的扩展,可以更加方面的读取int,long,字符等类型数据
   DataOutputStream
        writeInt()/writeDouble()/writeUTF()

 8)BufferedInputStream&BufferedOutputStream
 这两个流类位IO提供了带缓冲区的操作,一般打开文件进行写入
 或读取操作时,都会加上缓冲,这种流模式提高了IO的性能
 从应用程序中把输入放入文件,相当于将一缸水倒入到另一个缸中:
 FileOutputStream--->write()方法相当于一滴一滴地把水“转移”过去
 DataOutputStream-->writeXxx()方法会方便一些,相当于一瓢一瓢把水“转移”过去
 BufferedOutputStream--->write方法更方便,相当于一飘一瓢先放入桶中,再从桶中倒入到另一个缸中,性能提高了

(四.1)单字节读取---FileInputStream   ----> in.read()

        /**
	 * 读取指定文件内容,按照16进制输出到控制台
	 * 并且每输出10个byte换行
	 * @param fileName
	 * 单字节读取不适合大文件,大文件效率很低
	 */
	public static void printHex(String fileName)throws IOException{
		//把文件作为字节流进行读操作
		FileInputStream in = new FileInputStream(fileName);
		int b ;
		int i = 1;
		while((b = in.read())!=-1){
			if(b <= 0xf){
				//单位数前面补0
				System.out.print("0");
			}
			System.out.print(Integer.toHexString(b)+"  ");
			if(i++%10==0){
				System.out.println();
			}
		}
		in.close();
	}
(四.2)批量读取---FileInputStream  ----> in.read(buf,0,buf.length)
/**
	 * 批量读取,对大文件而言效率高,也是我们最常用的读文件的方式
	 * @param fileName
	 * @throws IOException
	 */
	public static void printHexByByteArray(String fileName)throws IOException{
		FileInputStream in = new FileInputStream(fileName);
		byte[] buf = new byte[8 * 1024];
		/*从in中批量读取字节,放入到buf这个字节数组中,
		 * 从第0个位置开始放,最多放buf.length个 
		 * 返回的是读到的字节的个数
		*/
		/*int bytes = in.read(buf,0,buf.length);//一次性读完,说明字节数组足够大
		int j = 1; 
		for(int i = 0; i < bytes;i++){   // 注意是 <span style="font-family: Arial, Helvetica, sans-serif;">i < bytes</span>
			System.out.print(Integer.toHexString(buf[i] & 0xff)+"  ");
			if(j++%10==0){
				System.out.println();
			}
		}*/
	        int bytes = 0;
	        int j = 1;
	        while((bytes = in.read(buf,0,buf.length))!=-1){
		    for(int i = 0 ; i < bytes;i++){
			  System.out.print(Integer.toHexString(buf[i] & 0xff)+"  ");
			  if(j++%10==0){
				  System.out.println();
			  }
		    }
	        }
	  in.close();
	}
(四.3) FileOutputStream(File file, boolean append)   // boolean append 尾部追加

public class FileOutDemo1 {
	public static void main(String[] args) throws IOException {
		//如果该文件不存在,则直接创建,如果存在,删除后创建 !!!!
		FileOutputStream out = new FileOutputStream("demo/out.dat");
		out.write('A');//写出了'A'的低八位
		out.write('B');//写出了'B'的低八位
		int a = 10;    //write只能写八位,那么写一个int需要些4次每次8位
		out.write(a >>> 24);
		out.write(a >>> 16);
		out.write(a >>> 8);
		out.write(a);
		byte[] gbk = "中国".getBytes("gbk");
		out.write(gbk);
		out.close();
		
		IOUtil.printHex("demo/out.dat");
	}
}
文件拷贝1

/**
	 * 文件拷贝,字节批量读取
	 * @param srcFile
	 * @param destFile
	 * @throws IOException
	 */
	public static void copyFile(File srcFile,File destFile)throws IOException{
		if(!srcFile.exists()){
			throw new IllegalArgumentException("文件:"+srcFile+"不存在");
		}
		if(!srcFile.isFile()){
			throw new IllegalArgumentException(srcFile+"不是文件");
		}
		FileInputStream in = new FileInputStream(srcFile);
		FileOutputStream out = new FileOutputStream(destFile);
		byte[] buf = new byte[8*1024];
		int b ;
	        while((b = in.read(buf,0,buf.length))!=-1){
	    	   out.write(buf,0,b);
	    	   out.flush();//最好加上
	        }
	        in.close();
	        out.close();
		
	}

文件拷贝2

/**
	 * 单字节,不带缓冲进行文件拷贝
	 * @param srcFile
	 * @param destFile
	 * @throws IOException
	 */
	public static void copyFileByByte(File srcFile,File destFile)throws IOException{
		if(!srcFile.exists()){
			throw new IllegalArgumentException("文件:"+srcFile+"不存在");
		}
		if(!srcFile.isFile()){
			throw new IllegalArgumentException(srcFile+"不是文件");
		}
		FileInputStream in = new FileInputStream(srcFile);
		FileOutputStream out = new FileOutputStream(destFile);
		int c ;
		while((c = in.read())!=-1){
			out.write(c);
			out.flush();
		}
		in.close();
		out.close();
	}

(四.4)字节流之数据输入输出流DataOutputStream/DataInputStream

DataOutputStream/DataInputStream 对"流"功能的扩展,可以更加方面的读取int,long,字符等类型数据
   DataOutputStream
   writeInt()/writeDouble()/writeUTF()

DataOutputStream

public class DosDemo {
	public static void main(String[] args) throws IOException {
		String file = "demo/dos.dat";
		DataOutputStream dos = new DataOutputStream(
				         new FileOutputStream(file));
		dos.writeInt(10);
		dos.writeInt(-10);
		dos.writeLong(10l);
		dos.writeDouble(10.5);
		dos.writeUTF("中国");//采用utf-8编码写出
		dos.writeChars("中国");//采用utf-16be编码写出
		dos.close();
		IOUtil.printHex(file);
	}
}
<span style="font-family: monospace;font-size:18px; background-color: rgb(255, 255, 51);"><strong>DataInputStream</strong></span>
public class DisDemo {
	public static void main(String[] args) throws IOException{
	   String file = "demo/dos.dat";
	   IOUtil.printHex(file);
	   DataInputStream dis = new DataInputStream(new FileInputStream(file));
	   int i = dis.readInt();
	   System.out.println(i);
	   i = dis.readInt();
	   System.out.println(i);
	   long l = dis.readLong();
	   System.out.println(l);
	   double d = dis.readDouble();
	   System.out.println(d);
	   String s = dis.readUTF();
	   System.out.println(s);
	   dis.close();
	}
}
(四.5)字节缓冲流
 BufferedInputStream&BufferedOutputStream
 这两个流类位IO提供了带缓冲区的操作,一般打开文件进行写入或读取操作时,都会加上缓冲,这种流模式提高了IO的性能
 从应用程序中把输入放入文件,相当于将一缸水倒入到另一个缸中:
 FileOutputStream--->write()方法相当于一滴一滴地把水“转移”过去
 DataOutputStream-->writeXxx()方法会方便一些,相当于一瓢一瓢把水“转移”过去
 BufferedOutputStream--->write方法更方便,相当于一飘一瓢先放入桶中,再从桶中倒入到另一个缸中,性能提高了

        /**
	 * 进行文件的拷贝,利用带缓冲的字节流
	 * @param srcFile
	 * @param destFile
	 * @throws IOException
	 */
	public static void copyFileByBuffer(File srcFile,File destFile)throws IOException{
		if(!srcFile.exists()){
			throw new IllegalArgumentException("文件:"+srcFile+"不存在");
		}
		if(!srcFile.isFile()){
			throw new IllegalArgumentException(srcFile+"不是文件");
		}
		BufferedInputStream bis = new BufferedInputStream( new FileInputStream(srcFile));
		BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(destFile));
		int c ;
		while((c = bis.read())!=-1){
			bos.write(c);
			bos.flush();//刷新缓冲区 一定要写,否则写入不到文件中去
		}
		bis.close();
		bos.close();
	}

文件拷贝效率(3.62M)  单字节  通过byte数组批量字节读取(最快  带有缓冲区



(5) 字符流

 1) 编码问题
 2)认识文本和文本文件
 java的文本(char)是16位无符号整数,是字符的unicode编码(双字节编码)
 文件是byte byte byte ...的数据序列
 文本文件是文本(char)序列按照某种编码方案(utf-8,utf-16be,gbk)序列化为byte的存储结果
3)字符流(Reader Writer)---->操作的是文本文本文件
字符的处理,一次处理一个字符
字符的底层任然是基本的字节序列
字符流的基本实现
   InputStreamReader   完成byte流解析为char流,按照编码解析
   OutputStreamWriter  提供char流到byte流,按照编码处理  
   
   FileReader/FileWriter
   字符流的过滤器
   BufferedReader   ---->readLine 一次读一行
   BufferedWriter/PrintWriter   ---->写一行    

(5.1)InputStreamReader &OutputStreamWriter   

public class IsrAndOswDemo {
	public static void main(String[] args) throws IOException {
		FileInputStream in = new FileInputStream("e:\\javaio\\imoocutf8.txt");
		InputStreamReader isr = new InputStreamReader(in, "utf-8");// 默认项目的编码,操作的时候,要写文件本身的编码格式

		FileOutputStream out = new FileOutputStream(
				"e:\\javaio\\imoocutf81.txt");
		OutputStreamWriter osw = new OutputStreamWriter(out, "utf-8");
		/*
		 * int c ; while((c = isr.read())!=-1){ System.out.print((char)c); }
		 */
		char[] buffer = new char[8 * 1024];
		int c;
		/*
		 * 批量读取,放入buffer这个字符数组,从第0个位置开始放置,最多放buffer.length个 返回的是读到的字符的个数
		 */
		while ((c = isr.read(buffer, 0, buffer.length)) != -1) {
			String s = new String(buffer, 0, c);
			System.out.print(s);
			osw.write(buffer, 0, c);
			osw.flush();
		}
		isr.close();
		osw.close();
	}
}

(5.2)字符流之文件读写流  FileReader FileWriter  (构造函数没有 可以指定  编码的 地方 )

public class FrAndFwDemo {
	public static void main(String[] args) throws IOException{
		FileReader fr = new FileReader("e:\\javaio\\imooc.txt");
		FileWriter fw = new FileWriter("e:\\javaio\\imooc2.txt");
		//FileWriter fw = new FileWriter("e:\\javaio\\imooc2.txt",true);
		char[] buffer = new char[2056];
		int c ;
		while((c = fr.read(buffer,0,buffer.length))!=-1){
			fw.write(buffer,0,c);
			fw.flush();
		}
		fr.close();
		fw.close();
	}
}

(5.2) 字符流的过滤器

BufferedReader                   除了基本的之外 还可以一次读一行

BufferedWriter/PrintWriter    除了基本的之外 还可以一次写一行

BufferedWriter/PrintWriter    区别

public class BrAndBwOrPwDemo {
	public static void main(String[] args) throws IOException{
		 //对文件进行读写操作 
		BufferedReader br = new BufferedReader(
				new InputStreamReader(
						new FileInputStream("e:\\javaio\\imooc.txt")));
		/*BufferedWriter bw = new BufferedWriter(
				new OutputStreamWriter(
						new FileOutputStream("e:\\javaio\\imooc3.txt")));*/
		PrintWriter pw = new PrintWriter("e:\\javaio\\imooc4.txt");
		//PrintWriter pw1 = new PrintWriter(outputStream,boolean autoFlush);
		String line ;
		while((line = br.readLine())!=null){
			System.out.println(line);//一次读一行,并不能识别换行
			/*bw.write(line);
			//单独写出换行操作
			bw.newLine();//换行操作
			bw.flush();*/
			pw.println(line);
			pw.flush();
		}
		br.close();
		//bw.close();
		pw.close();
	}
}

(6) 对象的序列化和反序列化

3.对象的序列化,反序列化
1)对象序列化,就是将Object转换成byte序列,反之叫对象的反序列化 
2)序列化流(ObjectOutputStream),是过滤流----writeObject
   反序列化流(ObjectInputStream)---readObject

3)序列化接口(Serializable)
   对象必须实现序列化接口 ,才能进行序列化,否则将出现异常
   这个接口,没有任何方法,只是一个标准

 
4) transient关键字
       private void writeObject(java.io.ObjectOutputStream s)
       throws java.io.IOException
private void readObject(java.io.ObjectInputStream s)
       throws java.io.IOException, ClassNotFoundException
       
   分析ArrayList源码中序列化和反序列化的问题
 
5)序列化中 子类和父类构造函数的调用问题
 
 1.对象的序列化

public class ObjectSeriaDemo1 {
	public static void main(String[] args) throws Exception{
		String file = "demo/obj.dat";
		//1.对象的序列化
		/*ObjectOutputStream oos = new ObjectOutputStream(
				new FileOutputStream(file));
		Student stu = new Student("10001", "张三", 20);
		oos.writeObject(stu);
		oos.flush();
		oos.close();*/
		ObjectInputStream ois = new ObjectInputStream(
				new FileInputStream(file));
		Student stu = (Student)ois.readObject();
		System.out.println(stu);
		ois.close();
	}
}
.transient及ArrayList源码分析
transient关键字     

 //该元素不会进行jvm默认的序列化,也可以自己完成这个元素的序列化

那么transient的意义何在?分析ArrayList源码中序列化和反序列化的问题   ArrayList中存放数据的数组使用transient修饰,其序列化是通过writeObject方法实现的,其代码如下图。 只是对有效大小元素进行了序列化。其他的为存放元素的空位置 没有进行序列化。 如果不适用transient修饰,数组的全部都会被序列化。  所以自己完成序列化 只把有效元素序列化。


        private void writeObject(java.io.ObjectOutputStream s)

        throws java.io.IOException
private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOException, ClassNotFoundException 

public class Student implements Serializable{
	private String stuno;
	private String stuname;
	//该元素不会进行jvm默认的序列化,也可以自己完成这个元素的序列化
	private transient int stuage;  
	
	public Student(String stuno, String stuname, int stuage) {
		super();
		this.stuno = stuno;
		this.stuname = stuname;
		this.stuage = stuage;
	}

	public String getStuno() {
		return stuno;
	}
	public void setStuno(String stuno) {
		this.stuno = stuno;
	}
	public String getStuname() {
		return stuname;
	}
	public void setStuname(String stuname) {
		this.stuname = stuname;
	}
	public int getStuage() {
		return stuage;
	}
	public void setStuage(int stuage) {
		this.stuage = stuage;
	}
	@Override
	public String toString() {
		return "Student [stuno=" + stuno + ", stuname=" + stuname + ", stuage="
				+ stuage + "]";
	}
	 private void writeObject(java.io.ObjectOutputStream s)
		        throws java.io.IOException{
		 s.defaultWriteObject();//把jvm能默认序列化的元素进行序列化操作
		 s.writeInt(stuage);//自己完成stuage的序列化
	 }
	 private void readObject(java.io.ObjectInputStream s)
		        throws java.io.IOException, ClassNotFoundException{
		  s.defaultReadObject();//把jvm能默认反序列化的元素进行反序列化操作
		  this.stuage = s.readInt();//自己完成stuage的反序列化操作
	}
}

(6.3) 序列化中子父类构造函数问题

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

一个类实现了序列化Serializable接口,其所有的子类都可以进行序列化。

结论:

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

public class ObjectSeriaDemo2 {
	public static void main(String[] args) throws Exception {
		/*
		 * ObjectOutputStream oos = new ObjectOutputStream( new
		 * FileOutputStream("demo/obj1.dat")); Foo2 foo2 = new Foo2();
		 * oos.writeObject(foo2); oos.flush(); oos.close();
		 */

		// 反序列化是否递归调用父类的构造函数
		/*
		 * ObjectInputStream ois = new ObjectInputStream( new
		 * FileInputStream("demo/obj1.dat")); Foo2 foo2 =
		 * (Foo2)ois.readObject(); System.out.println(foo2); ois.close();
		 */
		/*
		 * ObjectOutputStream oos = new ObjectOutputStream( new
		 * FileOutputStream("demo/obj1.dat")); Bar2 bar2 = new Bar2();
		 * oos.writeObject(bar2); oos.flush(); oos.close();
		 */
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
				"demo/obj1.dat"));
		Bar2 bar2 = (Bar2) ois.readObject();
		System.out.println(bar2);
		ois.close();
		/*
		 * 对子类对象进行反序列化操作时, 如果其父类没有实现序列化接口 那么其父类的构造函数会被调用
		 */
	}
}

/*
 * 一个类实现了序列化接口,那么其子类都可以进行序列化
 */
class Foo implements Serializable {
	public Foo() {
		System.out.println("foo...");
	}
}

class Foo1 extends Foo {
	public Foo1() {
		System.out.println("foo1...");
	}
}

class Foo2 extends Foo1 {
	public Foo2() {
		System.out.println("foo2...");
	}
}

class Bar {
	public Bar() {
		System.out.println("bar");
	}
}

class Bar1 extends Bar {
	public Bar1() {
		System.out.println("bar1..");
	}
}

class Bar2 extends Bar1 implements Serializable {
	public Bar2() {
		System.out.println("bar2...");
	}
}











文章是慕课网IO课程的http://www.imooc.com/learn/123 听课笔记。感谢相关的老师。最近重新学了se。还是做下笔记。加深印象。

发布了55 篇原创文章 · 获赞 4 · 访问量 21万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章