(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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章