04- IO流

IO流基礎

硬盤 內存

一臺電腦啓動過程

把硬盤中的操作系統 讀取到內存中

關機 把內存中的東西清空 , 保存到硬盤中

把電腦的內存當成 程序執行的位置

硬盤中的文件讀取到內存中 爲輸入流

內存中的文件寫出到硬盤中 爲輸出流

File

文件或目錄路徑名的抽象表示

常用屬性

separator

// 與系統相關的默認名稱 - 分隔符字符,以方便的方式表示爲字符串
windows  "\"  
linux "/" 
在Windows 中 可以使用  / 代替 \

pathSeparator

回顧 配置java的 環境變量 , 多個路徑之間使用 ; 分隔
windows 爲 分號
linux 爲 冒號

構造方法

  • new File(文件或目錄的路徑);
  • new File(文件或目錄的父級路徑(字符串), 文件或目錄的子級路徑);
  • new File(文件或目錄的父級路徑(文件對象), 文件或目錄的子級路徑);

絕對路徑和相對路徑

絕對路徑

1- 盤符 D:/a.txt
2- 以 "/" 開頭 表示當前項目所在的磁盤的根目錄

相對路徑

直接寫文件名或目錄名 相當於表示文件本身所在的項目位置

常用方法

查看

getAbsolutePath()  返回此抽象路徑名的絕對形式。
getPath()  將此抽象路徑名轉換爲路徑名字符串。
getName()  返回由此抽象路徑名錶示的文件或目錄的名稱。
getParent()  返回此抽象路徑名的父 null的路徑名字符串,
		如果此路徑名未命名爲父目錄,則返回null。
length()  返回由此抽象路徑名錶示的文件的長度。
File f1 = new File("D:\\a.txt");
System.out.println(f1.getPath());  // D:\a.txt
System.out.println(f1.getAbsolutePath());  //D:\a.txt
// 使用相對路徑
File f2 = new File("a.txt");
System.out.println(f2.getPath());  // a.txt
System.out.println(f2.getAbsolutePath());  //D:\IDEA\hello_plus\a.txt

判斷

exists()  測試此抽象路徑名錶示的文件或目錄是否存在。 
isAbsolute()  測試這個抽象路徑名是否是絕對的。
isDirectory()  測試此抽象路徑名錶示的文件是否爲目錄。 
isFile()  測試此抽象路徑名錶示的文件是否爲普通文件。 

新增刪除

mkdir() 創建由此抽象路徑名命名的目錄。
mkdirs() 創建由此抽象路徑名命名的目錄,包括任何必需但不存在的父目錄。
createNewFile() 
當且僅當具有該名稱的文件尚不存在時,原子地創建一個由該抽象路徑名命名的新的空文件。 
delete() 
刪除由此抽象路徑名錶示的文件或目錄。 

獲取子級目錄

String[] list() 
返回一個字符串數組,命名由此抽象路徑名錶示的目錄中的文件和目錄。 
File[] listFiles() 
返回一個抽象路徑名數組,表示由該抽象路徑名錶示的目錄中的文件。 
File dir = new File("D:\\601- 壁紙");
String[] list = dir.list();
System.out.println(Arrays.toString(list));
File[] files = dir.listFiles();
for(File file : files ){
    System.out.println(file.getName());
}

遍歷文件夾的方法(包括子文件夾)

// 遍歷文件夾中的所有子文件
public static void showDir(File dir){
    //如果 該文件是一個文件夾, 纔可以有子文件夾
    if(dir.isDirectory()){
        // 獲取子目錄
        File[] files = dir.listFiles();
        for(File file : files){
            showDir(file);
        }
    }else{
        System.out.println(dir.getName());
    }
}

遞歸

在方法中調用 方法本身

計算 n的階乘

public static int jc(int n){  // 5
    int jc = 1;
    for(int i = 1; i <= n ; i++){
        jc *= i;
    }
    return jc;
}
// 第二種方案
// jc(n) 就是 n的階乘
// jc(5) = 5 * jc(4)
// 3! = 3 * 2 * 1
// 4! = 4 * 3!
public static int jc(int n){
    if(n == 1) {return 1;}
    return n * jc(n-1);
}

遞歸練習題

/* 
	一球從100米高度自由落下,每次落地後反跳回原高度的一半;再落下,求它在 第10次落地時,共經過多少米?第10次反彈多高?
*/ 
public class Demo6 {
    public static void main(String[] args) {
        System.out.println(ft(10));
        // 100 + 每一次彈起的高度 * 2
        int sum = 100;
        for(int i = 1 ; i < 2;i++){
            sum += ft(i)*2;
        }

    }
    // 小球第n 次反彈高度爲 多少
    public static double ft(int n){
        if(n == 0){return 100;}
        return ft(n-1)/2;
    }
}
把一個數組裏的數進行組合全部列出,比如1和2列出來爲1,2,12,21
水仙花數在數學上的定義是:指一個三位數,如果它的各位數字的立方和等於其本身,則這個三位數爲水仙花數, 計算1000 之內的所有水仙花數

IO流

流是一組有序的數據序列
以先進先出方式發送信息的通道

輸入入流和輸出流 站在程序的角度上

從數據源 到程序 輸入流

從程序到數據源 輸出流

分類

流的方向

  • 輸入流 InputStream 和 Reader
  • 輸出流 OutputStream 和 Writer

按照處理數據單元劃分

  • 字節流 ( 8位通用字節流)
    • 字節輸入流 InputStream
    • 字節輸出流 OutputStream
  • 字符流 (16位unicode字符流)
    • 字符輸入流 Reader
    • 字符輸出流 Writer

字節流輸入流 InputStream

這個抽象類是表示輸入字節流的所有類的超類。
public abstract class InputStream{}
public class FileInputStream extends InputStream{}

FileInputStream

構造方法

FileInputStream(File file) 
通過打開與實際文件的連接創建一個 FileInputStream ,該文件由文件系統中的 File對象 file命名。   
FileInputStream(String name) 
通過打開與實際文件的連接來創建一個 FileInputStream ,該文件由文件系統中的路徑名 name命名。  

方法

int read() 
從該輸入流讀取一個字節的數據。  
int read(byte[] b) 
從該輸入流讀取最多 b.length個字節的數據爲字節數組。  
int read(byte[] b, int off, int len) 
從該輸入流讀取最多 len字節的數據爲字節數組。  
close() 
關閉此文件輸入流並釋放與流相關聯的任何系統資源。 
  • read() 每次讀取一個字節, 讀取完成之後將指針向後移動一位

當我們在查看某個電話本的時候, 將手指當做一個 防止看錯行的工具

看完一行, 手指向下一行移動

  • 當指針指向數據的時, 返回數據對應的字節值

數據讀取完畢, 沒有數據指向, 返回 -1

案例

/*
使用程序 讀取D:\\a.txt 文件中的內容
1- 創建該文件的對象
2- 使用流 建立起與文件之間的通道
3- 讀取文件
4- 關閉通道
 */

原始寫法

File file = new File("D:\\a.txt");
FileInputStream fis = new FileInputStream(file);
// InputStream 字節輸入流 , read() 每次讀取一個字節
  int i = 0;
  // 這種寫法 會出現多打印一次 -1 的情況
  while(i != -1){
      i  = fis.read();  // 沒有數據 返回 -1
      System.out.println(i);
  }
  fis.close();

優化

FileInputStream fis = new FileInputStream("D:\\a.txt");
int i ;
while( (i = fis.read()) != -1){
    System.out.print((char)i);
}
fis.close();

使用數組優化

// fis.read() // 每次讀取一個字節, 效率非常低, 想要一次讀取多個字節
FileInputStream fis = new FileInputStream("D:\\a.txt");
// 使用工具
byte [] bys = new byte[1024];
int len;  // 數組中真實數據的長度
while( (len = fis.read(bys)) != -1){
    for(int i = 0 ; i< len; i++){
        System.out.print((char)bys[i]);
    }
}

字節輸出流

構造方法

FileOutputStream(String name) 
創建文件輸出流以指定的名稱寫入文件。  
FileOutputStream(String name, boolean append) 
創建文件輸出流以指定的名稱寫入文件。  
FileOutputStream(File file) 
創建文件輸出流以寫入由指定的 File對象表示的文件。  
FileOutputStream(File file, boolean append) 
創建文件輸出流以寫入由指定的 File對象表示的文件。 

注意點

1.兩種一個參數的構造方法在向文件寫數據時將覆蓋文件中原有的內容
	帶有boolean值得參數, true 表示 不會覆蓋, 在原有內容的基礎之上添加
2.創建FileOutputStream實例時,如果相應的文件並不存在,則會自動創建一個空的文件

方法

void write(byte[] b) 
將 b.length個字節從指定的字節數組寫入此文件輸出流。  
void write(byte[] b, int off, int len) 
將 len字節從位於偏移量 off的指定字節數組寫入此文件輸出流。  
void write(int b) 
將指定的字節寫入此文件輸出流。 
void close() 
關閉此文件輸出流並釋放與此流相關聯的任何系統資源。  
public void flush()throws IOException
刷新此輸出流並強制任何緩衝的輸出字節被寫出。

案例

// 把程序中的一個字符串 輸出到 硬盤中
// 1- 建立程序到硬盤文件的通道
// 2- 調用方法 寫出數據
// 3- 關閉資源
String s = "Hello outputStream";
FileOutputStream fos = new FileOutputStream("D:\\out.txt");
// 把該字符串輸出到硬盤
// write 一次只能輸出一個字節
byte[] bytes = s.getBytes();
// 遍歷數組並輸出
for(byte by : bytes){
    fos.write(by);
}
fos.close();

程序優化

String s = "Hello outputStream";
FileOutputStream fos = new FileOutputStream("D:\\out.txt");
// 把該字符串輸出到硬盤
// write 一次只能輸出一個字節
byte[] bytes = s.getBytes();
fos.write(bytes);
fos.close();
String s = "Hello outputStream";
FileOutputStream fos = new FileOutputStream("D:\\out.txt");
// 把該字符串輸出到硬盤
// write 一次只能輸出一個字節
byte[] bytes = s.getBytes();
fos.write(bytes,0,5);
fos.close();

綜合案例

文件複製

// 將D:\\a.txt 文件 複製到 C:\\a.txt 中

初級版本

// 把 source 源文件 複製到 target 目標文件
public static void copy(String source , String target) throws IOException {
    // 1- 建立輸入和輸出流
    // 2- 通過輸入流把文件內容讀取到 程序
    // 3- 通過程序寫出到目標文件
    // 4- 關閉資源
    FileInputStream fis = new FileInputStream(source);
    FileOutputStream fos = new FileOutputStream(target);
    // 使用單個字節的方式讀取
    int i;
    while( (i = fis.read()) != -1){
        // i 是字節輸入流 讀取到的 字節
        fos.write(i);
    }
    fos.close();
    fis.close();
}

查看該程序的運行時間

// 把 source 源文件 複製到 target 目標文件
public static void copy1(String source , String target) throws IOException {
    FileInputStream fis = new FileInputStream(source);
    FileOutputStream fos = new FileOutputStream(target);
    // 使用單個字節的方式讀取
    int i;
    long oldTime = System.currentTimeMillis();
    while( (i = fis.read()) != -1){
        // i 是字節輸入流 讀取到的 字節
        fos.write(i);
    }
    long newTime = System.currentTimeMillis();
    System.out.println("該程序運行所用時間爲: "+(newTime-oldTime)+"毫秒");
    fos.close();
    fis.close();
}
// 一個5.64M的圖片 所花時間爲 46454毫秒

使用數組優化程序

// 把 source 源文件 複製到 target 目標文件
public static void copy2(String source , String target) throws IOException {
    FileInputStream fis = new FileInputStream(source);
    FileOutputStream fos = new FileOutputStream(target);
    // 使用單個字節的方式讀取
    // fis.read()  返回值 是 輸入流讀取的 一個字節
    // fis.read(數組) 返回值 是輸入流讀取到的字節數組的長度
    byte [] bys = new byte[1024];
    int len;
    long oldTime = System.currentTimeMillis();
    while( (len = fis.read(bys)) != -1){
        // i 是字節輸入流 讀取到的 字節
        fos.write(bys,0,len);
    }
    long newTime = System.currentTimeMillis();
    System.out.println("該程序運行所用時間爲: "+(newTime-oldTime)+"毫秒");
    fos.close();
    fis.close();
}

內部處理異常

public static void copy2(String source , String target){
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try{
       fis = new FileInputStream(source);
       fos = new FileOutputStream(target);
        // 使用單個字節的方式讀取
        // fis.read()  返回值 是 輸入流讀取的 一個字節
        // fis.read(數組) 返回值 是輸入流讀取到的字節數組的長度
        byte [] bys = new byte[1024];
        int len;
        long oldTime = System.currentTimeMillis();
        while( (len = fis.read(bys)) != -1){
            // i 是字節輸入流 讀取到的 字節
            fos.write(bys,0,len);
        }
        long newTime = System.currentTimeMillis();
        System.out.println("該程序運行所用時間爲: "+(newTime-oldTime)+"毫秒");
    }catch(Exception e){
        System.out.println(e.getMessage());
    }finally{
        if(fos != null){
            try {
                fos.close();// 垃圾回收機制
            } catch (IOException e) {
                fos = null;
            }
        }
        if(fis != null){
            try {
                fis.close();
            } catch (IOException e) {
                fis = null;
            }
        }
    }
}

多文件複製案例

可變參數

// 可變參數優化
public static void add(int ...arr){
    int sum = 0;
    for(int i : arr){
        sum += i;
    }
    System.out.println(sum);
}

多文件複製

public static void copy3(String targetDir, String ...list){
    FileInputStream fis = null;
    FileOutputStream fos = null;
    for(String sfile : list){
        try{
            // 根據集合中所保存的 文件的路徑創建文件對象
            File source = new File(sfile);
            String fileName = source.getName();
            File tarDir = new File(targetDir);
            if(!tarDir.exists()){
                tarDir.mkdirs();
            }
            File target = new File(tarDir,fileName);
            fis = new FileInputStream(source);
            fos = new FileOutputStream(target);
            // 使用單個字節的方式讀取
            // fis.read()  返回值 是 輸入流讀取的 一個字節
            // fis.read(數組) 返回值 是輸入流讀取到的字節數組的長度
            byte [] bys = new byte[1024];
            int len;
            long oldTime = System.currentTimeMillis();
            while( (len = fis.read(bys)) != -1){
                // i 是字節輸入流 讀取到的 字節
                fos.write(bys,0,len);
            }
            long newTime = System.currentTimeMillis();
            System.out.println("該程序運行所用時間爲: "+(newTime-oldTime)+"毫秒");
        }catch(Exception e){
            System.out.println(e.getMessage());
        }finally{
            if(fos != null){
                try {
                    fos.close();// 垃圾回收機制
                } catch (IOException e) {
                    fos = null;
                }
            }
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    fis = null;
                }
            }
        }
    }
}

使用字節流讀取漢字

字符編碼

  • ASCII 128 個字節值 表示 控制字符和可打印字符
  • ISO-8859-1 西歐編碼
  • GB2312 / GBK / GB18030 中文編碼字符集
  • UTF-8 支持在全球範圍內使用
public static void hz() throws IOException {
    FileInputStream fis = new FileInputStream("D:\\a.txt");
    byte [] bys = new byte[100];
    int len ;
    while( (len = fis.read(bys)) != -1){
        // 字節數組 轉換爲 String字符串
        String s = new String(bys,"GBK");
        System.out.println(s);
    }
    fis.close();
}

轉換流

InputStreamReader

語法 可以在構建對象的時候, 通過傳入 字節輸入流的參數, 把字節流轉換爲字符流

還可以在構建對象的時候, 傳入編碼字符集

字節流和字符流的對比

字節類似於 玩具的每一個模塊

字符 類似於 一個小玩具, 是一個整體

public static void testGBK1()throws Exception{
    // 讀取GBK.txt 文件 把把內容顯示到控制檯
    FileInputStream fis = new FileInputStream("D:\\GBK.txt");
    // 把字節流 轉換爲字符流 , 依賴於一個字節流  ,參數二 爲指定格式的字符集
    InputStreamReader isr = new InputStreamReader(fis,"GBK");
    char [] chs = new char[1024];
    int len;
    while(  (len = isr.read(chs)) != -1){
        String s = new String(chs, 0, len);
        System.out.println(s);
    }
    fis.close();
}

字符流

字符輸入流

Reader

InputStreamReader 是 Reader 的一個子類

FileReader

構造方法

FileReader(File file) 
創建一個新的 FileReader ,給出 File讀取。 
FileReader(String fileName) 
創建一個新的 FileReader ,給定要讀取的文件的名稱。 

成員方法

int read() 
讀一個字符  
int read(char[] cbuf) 
將字符讀入數組。  
abstract int read(char[] cbuf, int off, int len) 
將字符讀入數組的一部分。  

案例

讀取文檔內容

public static void read1() throws Exception{
    FileReader fr = new FileReader("D:\\a.txt");
    int ch;
    while( (ch = fr.read()) != -1){
        System.out.print((char)ch);
    }
    fr.close();
}

優化

public static void read() throws Exception{
    FileReader fr = new FileReader("C:\\盜墓筆記.txt");
    char [] chs = new char[1024];
    int len;
    while( (len = fr.read(chs)) != -1){
        System.out.print(new String(chs,0,len));
    }
    fr.close();
}

字符輸出流

Writer

案例

// 文件複製案例
public static void copy() throws Exception{
    FileReader fr = new FileReader("C:\\盜墓筆記.txt");
    FileWriter fw = new FileWriter("D:\\盜墓.txt");
    char [] chs = new char[1024];
    int len;
    while( (len = fr.read(chs)) != -1){
        fw.write(chs,0,len);
    }
    fr.close();
    fw.close();
}

緩衝區增強流

BufferedReader

BufferedWriter

因爲 緩衝區功能是一個增強流, 需要依賴於原始流

通過構造方法, 把原始流進行增強

使用BufferedReader 讀取文件

public static void m1() throws  Exception{
    // 普通流
    FileReader fr = new FileReader("C:\\盜墓筆記.txt");
    // 把原始流進行增強
    BufferedReader br = new BufferedReader(fr);
    int ch;
    while( (ch = br.read()) != -1){
        System.out.print((char)ch);
    }
    // 只需要關閉外層流
    br.close();
}

特有方法

String readLine() 讀一行文字。 

點名程序案例

​ 數組

​ 集合

​ IO流 繼續優化

public static void main(String[] args) throws Exception {
    // 1- 讀取 文本中的所有內容
    // 2- 每一行作爲集合的一個元素
    // 3- 使用集合 書寫點名程序
    FileReader fis = new FileReader("C:\\Users\\Administrator\\四班.txt");
    BufferedReader br = new BufferedReader(fis);
    // 定義存放所有 姓名的集合
    List<String> list = new ArrayList<String>();
    String s = null;
    while( (s = br.readLine()) != null ){
        list.add(s);
    }
    br.close();
    System.out.println(list);
    Random random = new Random();
    Scanner input = new Scanner(System.in);
  do{
      int no = random.nextInt(list.size());
      System.out.println("恭喜-=-"+list.get(no));
      list.remove(no);
      System.out.println("是否繼續? 按0 繼續");
      int i = input.nextInt();
      // 集合中沒有數據了
      if(i != 0  || list.isEmpty()){
          System.out.println("點名完畢!");
          break;
      }
  }while(true);
}

四個文件拷貝案例

// 使用普通字符流
public static void copy() throws Exception{
    FileReader fr = new FileReader("C:\\盜墓筆記.txt");
    FileWriter fw = new FileWriter("D:\\盜墓.txt");
    int ch;
    long old = System.currentTimeMillis();
    while( (ch = fr.read()) != -1){
        fw.write(ch);
    }
    System.out.println("普通字符 所用時間: "+ (System.currentTimeMillis
    fr.close();
    fw.close();
}
//使用普通字符流+ 數組複製案例
public static void copy1() throws Exception{
    FileReader fr = new FileReader("C:\\盜墓筆記.txt");
    FileWriter fw = new FileWriter("D:\\盜墓.txt");
    char [] chs = new char[1024];
    int len;
    long old = System.currentTimeMillis();
    while( (len = fr.read(chs)) != -1){
        fw.write(chs,0,len);
    }
    System.out.println("普通字符數組所用時間: "+ (System.currentTimeMilli
    fr.close();
    fw.close();
}
// 使用緩衝流字符
public static void copy2() throws Exception{
    FileReader fr = new FileReader("C:\\盜墓筆記.txt");
    FileWriter fw = new FileWriter("D:\\盜墓.txt");
    BufferedReader br = new BufferedReader(fr);
    BufferedWriter bw = new BufferedWriter(fw);
    int ch;
    long old = System.currentTimeMillis();
    while( (ch = br.read()) != -1){
        bw.write(ch);
    }
    System.out.println("緩衝流 所用時間: "+ (System.currentTimeMillis(
    br.close();
    bw.close();
}
// 使用緩衝流 + 數組
public static void copy3() throws Exception{
    FileReader fr = new FileReader("C:\\盜墓筆記.txt");
    FileWriter fw = new FileWriter("D:\\盜墓.txt");
    BufferedReader br = new BufferedReader(fr);
    BufferedWriter bw = new BufferedWriter(fw);
    char [] chs = new char[1024];
    int len;
    long old = System.currentTimeMillis();
    while( (len = br.read(chs)) != -1){
        bw.write(chs,0,len);
    }
    System.out.println("緩衝數組流 所用時間: "+ (System.currentTimeMilli
    br.close();
    bw.close();
}

讀寫二進制文件

.java 經過編譯過程 得到的 .class 字節碼文件 (二進制文件)(可執行文件)

DataInputStream

DataoutputStream

FileOutputStream fos = new FileOutputStream("D:\\b.txt");
DataOutputStream dos = new DataOutputStream(fos);
FileInputStream fis = new FileInputStream("D:\\b.txt");
DataInputStream dis = new DataInputStream(fis);
dos.writeBoolean(true);
dos.writeByte(120);
dos.writeChar('丞');
// 要求讀的順序和寫出的順序一致, 纔不會有讀取異常的問題
boolean b = dis.readBoolean();
System.out.println(b);
byte b1 = dis.readByte();
System.out.println(b1);
char c = dis.readChar();
System.out.println(c);
dis.close();
dos.close();

打印流

PrintStream

// 重定向IO 
FileOutputStream fos = new FileOutputStream("D:\\c.txt",true);
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
System.out.println("456789");

序列化流

序列化 ObjectOutputStream

將對象的狀態寫入到特定的流中的過程
(將內存中的對象 持久化到硬盤中的過程)

反序列化 ObjectInputStream

從特定的流中獲取數據重新構建對象的過程
(把已經持久化到硬盤中的對象, 讀取到內存中的過程)

案例

public class Student implements Serializable {
    String name;
    int age;
    String address;
    public Student() {
    }
    public Student(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }
}

自定義類完成序列化功能

Student stu = new Student("張清華", 23, "青島");
// stu 保存到硬盤中
// 序列化
FileOutputStream fos = new FileOutputStream("D:\\d.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
// java.io.NotSerializableException: s0729.Student
// TreeSet 排序 一個類想要具有排序的功能 必須實現 排序的接口
// 一個類可以序列化到 硬盤中 也是一個功能, 需要實現一個接口
oos.writeObject(stu);
oos.close();

類的序列化由實現java.io.Serializable接口的類啓用。 不實現此接口的類將不會使任何狀態序列化或反序列化。 可序列化類的所有子類型都是可序列化的。 序列化接口沒有方法或字段,僅用於標識可串行化的語義。

  • 要求 被序列化的類 必須實現 Serializable接口
  • 使用ObjectOutputStream

反序列化

 public static void inobj() throws Exception{
     FileInputStream fis = new FileInputStream("D:\\d.txt");
     ObjectInputStream ois = new ObjectInputStream(fis);
     
     Object obj = ois.readObject();
     Student stu = (Student) obj;
     System.out.println(stu.name);
 }

問題1

student 對象 先序列化到 磁盤 , 修改Student 中的內容 , 再次進行反序列化

// java.io.InvalidClassException: s0729.Student;
// local class incompatible: stream classdesc serialVersionUID = -8030828354293478697,
// local class serialVersionUID = 4591992182167877374

每一個實現了 Serializable 接口的類, 都有一個 默認的 serialVersionUID

​ 當程序員改變了該類的中的內容時, 該ID 也會發生變化, 進行反序列化時 就會發生 UID 不一致的情況, 導致反序列化失敗

針對該問題發生的原因, 當類發生變化的時候, 不希望serialVersionUID 發生變化, 在類中自定義 serialVersionUID 變量 , 改爲 public static final 類型的

public static final long serialVersionUID = 1L;

問題2

如果希望一些保密字段不序列化到磁盤中

transient int age; // 聲明不需要序列化的成員域

問題3

寫出了多個不同對象, 需要按照寫出的順序讀取

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章