Java 持久化操作之 --io流與序列化

来源:https://www.cnblogs.com/lsy131479/archive/2018/04/06/8728724.html
-Advertisement-
Play Games

1)File類操作文件的屬性 1.File類的常用方法 1. 文件的絕對完整路徑:getAbsolutePath() 文件名:getName() 文件相對路徑:getPath() 文件的上一級目錄:getParent() 文件的大小為:length() 刪除文件:delete() 具體操作請參考如下 ...


1)File類操作文件的屬性

1.File類的常用方法

 

1.

文件的絕對完整路徑:getAbsolutePath()

文件名:getName()

文件相對路徑:getPath()

文件的上一級目錄:getParent()

文件的大小為:length()

刪除文件:delete()

具體操作請參考如下代碼:

package text;
import java.io.File;
import java.util.Scanner;
/**
 * 
 * @author: 房上的貓
 * 
 * @time: 下午7:55:16
 * 
 * @博客地址: https://www.cnblogs.com/lsy131479/
 *
 *        消費信息
 *
 */
public class TextFile {
    public static void main(String[] args) 
    {
        try {
            FileText();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void FileText() throws Exception
    {
        //1.實例化對象指定判斷路徑
        File file=new File("D:\\TextFile\\A.txt");
        //2.判斷A.txt是否存在
        if(file.exists())
        {
            //判斷是否是文件夾
            if(file.isDirectory())
            {
                System.out.println("這是一個文件夾");
            }else
            {
                System.out.println("當前文件存在");
                System.out.println("這是一個文件");
                System.out.println("文件的絕對完整路徑:"+file.getAbsolutePath());
                System.out.println("文件名:"+file.getName());
                System.out.println("文件相對路徑:"+file.getPath());
                System.out.println("文件的上一級目錄:"+file.getParent());
                System.out.println("文件的大小為:"+file.length());    
                Scanner input=new Scanner(System.in);
                System.out.println("請按1刪除此文件=======");
                if(input.nextInt()==1)
                {
                    boolean flog1= file.delete();
                    if(flog1)
                    {
                        System.out.println("刪除成功");
                    }
                }
            }
        }else
        {
            System.out.println("當前文件不存在---開始創建");
            //3.不存在則創建新文件
            boolean flog=file.createNewFile();
            if(flog)
            {
                System.out.println("文件創建成功");
            }    
        }
    }
}

 


2)IO流(堵塞型io)

 

 如何讀寫文件?

    分析:流是指一連串流動的字元,是以先進先出方式發送信息的通道

 輸入/輸出流於數據源:

    

 

java流的分類:

我們可以對它進行如下分類:

· 按處理的數據類型可分為位元組流字元流

· 按流的流向可分為輸入流(in)與輸出流(out)

· 按流的功能可分為節點流(Node)和過濾流(Filter)

 

 

在Java中,位元組流一般適用於處理位元組數據(諸如圖片、視頻),字元流適用於處理字元數據(諸如文本文件),但二者並沒有嚴格的功能劃分,因為有轉換流的存在,使得對於數據的處理變得更加靈活。 

1)位元組流讀寫文件

一般用於處理位元組數據,但位元組流採用ASCII編碼的,所以處理字元數據時容易出現中文亂碼

1. 輸入流

InputStream:此抽象類是表示位元組輸入流的所有類的超類(基類)

序號

方法描述

1

public final int read(byte[] r, int off, int len)throws IOException
從所包含的輸入流中將 len 個位元組讀入一個位元組數組中。如果len為-1,則返回已讀位元組數。

2

Public final int read(byte [] b)throws IOException
從所包含的輸入流中讀取一定數量的位元組,並將它們存儲到緩衝區數組 b 中。

3

1. public final Boolean readBooolean()throws IOException,

2. public final byte readByte()throws IOException,

3. public final short readShort()throws IOException

4. public final Int readInt()throws IOException

從輸入流中讀取位元組,返回輸入流中兩個位元組作為對應的基本數據類型返回值。

4

public String readLine() throws IOException
從輸入流中讀取下一文本行。

 

所有位元組輸入流都是以此類發散出來的,但此類是一個抽象類,不可被實例化,所以實際編程過程中,都是使用它發散出來的一些子類,下麵是輸入流的關係圖:

輸入流最常用的就是FileInputStream類:

 

1-1 文本文件的讀取:用FileInputStream

該流用於從文件讀取數據,它的對象可以用關鍵字 new 來創建。

·有多種構造方法可用來創建對象。

·可以使用字元串類型的文件名來創建一個輸入流對象來讀取文件:

·····InputStream f = new FileInputStream("C:/java/hello");

·也可以使用一個文件對象來創建一個輸入流對象來讀取文件。我們首先得使用 File() 方法來創建一個文件對象:

·····File f = new File("C:/java/hello");

·····InputStream out = new FileInputStream(f);

·創建了InputStream對象,就可以使用下麵的方法來讀取流或者進行其他的流操作。

    位元組流:

    基類:InputStream     子類:FileInputStream

    構造:

    FileInputStream(File file)   ||   FileInputStream(String name)

    

 

    方法:

    read() 按位元組讀    

    read(byte[] b) 讀到位元組數組緩衝區,數組存滿統一批次迴圈讀取

    read(byte[] b, int off, int len)  向數組存放時進行了限制,起始位置off和終止位置len

    int  available()   表示當前還剩多少個位元組未讀取

    

 

    註意:read方法返回 int 類型 返回讀入位元組數組的長度,如果讀取到文件末尾的時候,則返回-1

代碼演示按位元組讀取到控制台:

    四步走:1.導入相關類   2.創建位元組流對象   3.實現讀取文本文件的數據   4.關閉文件流對象

    測試文檔:

    使用Read()讀取

   廢話不多說 舉個慄子:

package text;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
public class FileInputStream01 {
    public static void main(String[] args) {
        //創建位元組流對象
        InputStream fls=null;
        try {
            fls=new FileInputStream("D://TextFile//A.txt");
            //實現讀取操作
            int data;//存儲讀取的位元組
            while((data=fls.read())!=-1)
            {
                //System.out.print(data);//讀取的是數字
                System.out.print((char)data);//讀取的是和文件一致
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            try {
                if(fls!=null)
                fls.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

 

 

修改代碼使用Read(byte[] b)讀取

 
int len;//存儲讀入數組的長度
byte[] wrods=new byte[1024];
while((len=fls.read(wrods))!=-1)
{
    System.out.print(new String(wrods,0,len));//String構造方法 把位元組byte[] 轉換成字元串形式,0代表截取起始位置,len表示截取終止位置
}

 

結果與上述一致:

 

1. 輸出流

OutputStream:此抽象類是表示位元組輸出流的所有類的超類(基類)

序號

方法描述

1

public final void write(byte[] w, int off, int len)throws IOException
將指定位元組數組中從偏移量 off 開始的 len 個位元組寫入此位元組數組輸出流。

2

Public final int write(byte [] b)throws IOException
將指定的位元組寫入此位元組數組輸出流。

3

1. public final void writeBooolean()throws IOException,

2. public final void writeByte()throws IOException,

3. public final void writeShort()throws IOException,

4. public final void writeInt()throws IOException

這些方法將指定的基本數據類型以位元組的方式寫入到輸出流。

4

Public void flush()throws IOException
  刷新此輸出流並強制寫出所有緩衝的輸出位元組。

5

public final void writeBytes(String s) throws IOException
將字元串以位元組序列寫入到底層的輸出流,字元串中每個字元都按順序寫入,並丟棄其高八位。

所有位元組輸出流都是以此類發散出來的,但此類是一個抽象類,不可被實例化,所以實際編程過程中,都是使用它發散出來的一些子類,下麵是輸出流的關係圖:

 

輸出流最常用的就是FileOutputStream 類:

1-2文本文件的寫入:用FileOutputStream 

該類用來創建一個文件並向文件中寫數據。

如果該流在打開文件進行輸出前,目標文件不存在,那麼該流會創建該文件。

有兩個構造方法可以用來創建 FileOutputStream 對象。

使用字元串類型的文件名來創建一個輸出流對象:

OutputStream f = new FileOutputStream("C:/java/hello")

也可以使用一個文件對象來創建一個輸出流來寫文件。我們首先得使用File()方法來創建一個文件對象:

File f = new File("C:/java/hello");

OutputStream f = new FileOutputStream(f);

    輸出流:

    基類:OutputStream

    子類:FileOutputStream  ..............

    構造:

        

 

    方法:

 

   廢話不多說 舉個慄子:

package text;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FileoutputStream {
    public static void main(String[] args) {
        //創建輸出位元組流
        OutputStream fls =null;
        try {
            //以File類對象作為參數,或 String name
            //構造一個參數
            //fls=new FileOutputStream(new File("D://TextFile//B.txt"));
            
            //file - 為了進行寫入而打開的文件。
            //append - 如果為 true,則將位元組寫入文件末尾處,而不是寫入文件開始處 
            fls=new FileOutputStream(new File("D://TextFile//B.txt"),true);
            //聲明要寫入的內容
            String name="我是測試字元串";
            //將字元串轉換為位元組數組
            byte[] words=name.getBytes();
            //寫入方法一
            //fls.write(words);
            //寫入方法二
            fls.write(words,0,words.length);
            System.out.println("寫入成功");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            try {
                fls.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

 

1-3文件讀取和寫入同步

   廢話不多說 舉個慄子:

package text;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FileoutputStream {
    public static void main(String[] args) {
        //創建輸出位元組流
        OutputStream fls =null;
        try {
            //以File類對象作為參數,或 String name
            //構造一個參數
            //fls=new FileOutputStream(new File("D://TextFile//B.txt"));
            
            //file - 為了進行寫入而打開的文件。
            //append - 如果為 true,則將位元組寫入文件末尾處,而不是寫入文件開始處 
            fls=new FileOutputStream(new File("D://TextFile//B.txt"),true);
            //聲明要寫入的內容
            String name="我是測試字元串";
            //將字元串轉換為位元組數組
            byte[] words=name.getBytes();
            //寫入方法一
            //fls.write(words);
            //寫入方法二
            fls.write(words,0,words.length);
            System.out.println("寫入成功");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            try {
                fls.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

 

2)字元流讀和緩衝流讀文件

    用BufferedReader 和 BufferedWriter讀寫文本文件//字元流

    或 FileReader

    字元編碼:ASCII碼   0~127  8位二進位數1個位元組。  16位二進位數表示一個字元 兩個位元組

    字元流:輸入流

    基類:Reader----FileReader

                

 

    構造:

            

 

    常用方法:

            

 

1)如果 使用位元組流讀取帶有漢字的文件會怎麼樣那

package text;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
public class FileInputStream01 {
    public static void main(String[] args) {
        //創建位元組流對象
        InputStream fls=null;
        try {
            fls=new FileInputStream("D://TextFile//A.txt");
            //實現讀取操作
            int data;//存儲讀取的位元組
            while((data=fls.read())!=-1)
            {
                //System.out.print(data);//讀取的是數字
                //System.out.println("還剩:"+fls.available()+"位元組未讀取");            
                System.out.print((char)data);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            try {
                if(fls!=null)
                fls.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

 

運行結果如下:

 

很明顯出現了亂碼

2)下麵使用FileReader字元流 Read()讀取文件,示例如下

package text;
/**
 * 使用字元流讀取文本文件
 *<p>Title:FileReaderDemo</p>
 *<p>Description:</p>
 *<p>Company:</p>
 * @author MLQ
 * @date 2018年3月7日 下午12:38:35
 */
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class FileReaderDemo {
    public static void main(String[] args) {
        //創建一個字元流對象
        Reader rd=null;
        try {
            rd=new FileReader("D://TextFile//A.txt");
            int word;//就收讀取的字元
            while((word=rd.read())!=-1)
            {
                System.out.print((char)word);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            if(rd!=null)
            {
                try {
                    rd.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

 

運行結果你會發現,亂碼消失了

 

3)下麵使用FileReader字元流 Read(char[] b)讀取文件,示例如下

      修改代碼如下:

 
package text;
/**
 * 使用字元流讀取文本文件
 *<p>Title:FileReaderDemo</p>
 *<p>Description:</p>
 *<p>Company:</p>
 * @author MLQ
 * @date 2018年3月7日 下午12:38:35
 */
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class FileReaderDemo {
    public static void main(String[] args) {
        //創建一個字元流對象
        Reader rd=null;
        StringBuffer sb=new StringBuffer();
        try {
            rd=new FileReader("D://TextFile//A.txt");
            int word;//就收讀取的字元
            char[] ch=new char[1024];
            while((word=rd.read(ch))!=-1)
            {
                sb.append(ch,0,word);
            }
            System.out.println(sb.toString());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            if(rd!=null)
            {
                try {
                    rd.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

 

運行結果如下:

 

溫馨提示:如果上述的代碼 sb.append(ch,0,word)  只寫數組的話,會把空格也會追加到 StringBuffer。讀取文

                  件的時候最後可能沒有寫滿數組

 

4)使用BufferedReader讀取文本文件

(增強)

    BufferedReader類是Reader類的子類

    bufferedReader類帶有緩衝區

    按行讀取內容的ReadLine()方法

    實現步驟:

    

 

    構造:

    

 

    方法:

    

 

演示代碼如下:

package text;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.*;
public class BufferedReader01 {
    public static void main(String[] args) {
        //創建一個字元流對象
        Reader rd=null;
        //創建BufferedReader對象
        BufferedReader bf=null;
        StringBuffer sb=new StringBuffer();
        try {
            rd=new FileReader("D://TextFile//A.txt");
            bf=new BufferedReader(rd);//傳入Reader對象
            String word=null;//接收返回值,讀到末尾返回null
            while((word=bf.readLine())!=null)
            {
                System.out.println(word);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally
        {
            if(rd!=null)
            {
                try {
                    rd.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bf!=null)
            {
                try {
                    bf.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

 

 

運行結果如下:

 

3)字元流讀和緩衝流寫文件

    基類:Write    子類:FileWrite

    

 

    構造:

    

 

    方法:

    

 

演示代碼如下:

3-1)使用FileWrite字元流寫入文檔

package text;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class FileWriter01 {
    public static void main(String[] args) {
        //創建寫入字元流
        Writer rd=null;
        try {
            rd=new FileWriter("D://TextFile//A.txt",true);
            String s="我是寫入測試編碼";
            rd.write(s);//寫入文檔
            rd.flush();//刷新緩衝區
            System.out.println("寫入成功");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            if(rd!=null)
            {
                try {
                    rd.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

 

3-2)使用BufferedWrite字元流寫入文件

        如何提高字元流寫文本文件的效率?

        解:使用FileWrite類與BufferReader類

        BufferedWrite類是Write類的子類

        BufferedWrite類帶有緩衝區

        步驟:

        

 

    構造:

    

 

    方法:

    

 

代碼演示如下:

 
package text;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class BufferedWrite01 {
    public static void main(String[] args) {
        //創建FileWrite 和 BufferedWrite 對象
        Writer wd=null;
        BufferedWriter bw=null;
        //創建FileReader  和 BufferedReader  對象
        Reader ed=null;
        BufferedReader br=null;
        try {
            wd=new FileWriter("D:\\TextFile\\A.txt");
            bw=new BufferedWriter(wd);
            bw.write("我是測試員");
            bw.newLine();//換行符方法
            bw.write("負責測試程式運行問題");
            bw.flush();
            System.out.println("成功寫入");
            //讀取文本文件信息
            ed=new FileReader("D:\\TextFile\\A.txt");
            br=new BufferedReader(ed);
            String word=null;
            while((word=br.readLine())!=null)
            {
                System.out.println(word);
            }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            if(wd!=null){
                try {
                    wd.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(ed!=null){
                try {
                    ed.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

 

4)使用數據流讀寫文件

    二進位文件讀寫:

    使用DataInputStream  和  DataOutputStream 讀寫二進位文件  //屬於位元組流

    DataInputStream  類:

        FileInputStream的子類

        與FileInputStream類結合使用讀取二進位文件

    DataOutputStream 類:

        FileOutputStream的子類

        與FileOutputStream類結合使用寫二進位文件

     

 

DataOutputStream:

 

演示代碼如下:

實現步驟:

 

[
package text;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@SuppressWarnings("unused")
public class DataInOutStream {
    @SuppressWarnings("resource")
    public static void main(String[] args)throws Exception {
        DataInputStream di=null;
        DataOutputStream ds=null;
        di=new DataInputStream(new FileInputStream("D://TextFile//dd.class"));
        ds=new DataOutputStream(new FileOutputStream("D://TextFile//coty.class"));
        int len;//接收讀取的位元組
        while((len=di.read())!=-1)
        {
            ds.write(len);
        }
        System.out.println("執行完畢");
        if(di!=null)
        di.close();
        ds.close();
    }
}

 

3)序列化和反序列化

    序列化和反序列化的過程

    

 

序列化的步驟:

    1.實現 Serializable 介面

    2.創建對象輸出流

    3.調用 writeObject()方法將對象寫入文件

    4.關閉對象輸出流

    使用集合保存對象,可以將集合中的所有對象序列化

    序列化構造和常用方法   

 

 

反序列化構造和常用方法

 

 

    實現序列化 和 反序列化 演示代碼:

package text;
import java.io.Serializable;
public class Student implements Serializable
  {
    /**
     * 記錄版本信息
     */
    private static final long serialVersionUID = 224961941376318131L;
    private String name;
    private int age;
    transient private String sex;
    public Student(String name, int age, String sex) {
        super();
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the age
     */
    public int getAge() {
        return age;
    }
    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }
    /**
     * @return the sex
     */
    public String getSex() {
        return sex;
    }
    /**
     * @param sex the sex to set
     */
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    public void show()
    {
        System.out.println("我叫:"+this.getName()+"年齡:"+this.getAge()+"性別:"+this.getSex());
    }
}
Student 類

 

package text;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class TextStudent {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        //創建序列化對象
        ObjectOutputStream xl=null;
        //創建反序列化對象
        ObjectInputStream xl1=null;
        //初始化要序列化的對象
        Map<String,Student> map=new HashMap<String,Student>();
        Map<String,Student> map1=new HashMap<String,Student>();
        Student stu=new Student("小明",18,"男");
        Student stu1=new Student("小紅",18,"女");
        map.put(stu.getName(), stu);
        map.put(stu1.getName(), stu1);
        try {
            xl=new ObjectOutputStream(new FileOutputStream("D://TextFile//xuliehua.bin"));
            xl1=new ObjectInputStream(new FileInputStream("D://TextFile//xuliehua.bin"));
            xl.writeObject(map);
            System.out.println("寫入成功");
            //反序列化文件輸出到控制台
            map1=(Map<String,Student>)xl1.readObject();
            Set<String> key=map1.keySet();
            Iterator<String> l=key.iterator();
            while(l.hasNext())
            {
                String keys=l.next();
                map1.get(keys).show();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            if(xl!=null){
                try {
                    xl.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                e.printStackTrace();
                }
            }
            if(xl1!=null)
            {
                try {
                    xl1.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }
}
測試類

提示:如果不希望Student類某一屬性被序列化可使用 transient 修飾

 

 biu ~biu ~ biu ~  

註:最後在提一句:使用序列化操作時,一定要將準備序列化的類或數據聲明為可序列化操作!!!!

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 內容:j集合頂層共性的方法,集合下的兩個分支List和Set集合下的小分支——LinkedList、ArrayList、TreeSet、HashSet 在util包###頂層的共性介面collection方法 兩種集合List:有序(存入的順序和取出的順序一致)。有索引,允許重覆元素Set:不允許重 ...
  • at org.apache.ibatis.binding.MapperMethod$SqlCommand.(MapperMethod.java:225) at org.apache.ibatis.binding.MapperMethod.(MapperMethod.java:48) at org.a... ...
  • 1. 新建(NEW):新創建了一個線程對象。 2. 可運行(RUNNABLE):線程對象創建後,其他線程(比如main線程)調用了該對象的start()方法。該狀態的線程位於可運行線程池中,等待被線程調度選中,獲取cpu 的使用權 。 3. 運行(RUNNING):可運行狀態(runnable)的線 ...
  • 內容:協程 作用:實現高併發,提高效率##################################################################yield支持下的協程協程是用戶輕量級線程好處:1、沒有線程切換2、無需原子操作鎖定及同步開銷3、高併發+高擴展+低成本:一個cp ...
  • char int :有符號 uint:無符號 ...
  • 1)有關XML簡介 XML(EXtensible Markup Language)可擴展標記語言 特點:XML與操作系統、編程語言的開發平臺無關 實現不同系統之間的數據交換 作用:數據交換、配置應用程式和網站 大致文檔結構: XML文檔內容由一系列標簽元素組成: XML編寫註意事項: 所有XML元素 ...
  • 1. Java既是編譯型語言,又是解釋型語言 java源文件首先需要通過javac編譯生成尾碼名為.class的位元組碼文件(與平臺無關,只面向JVM),然後使用Java虛擬機將位元組碼解釋成特定平臺上的機器碼運行。 2. Java虛擬機JVM 不同平臺上的JVM不同,但是都提供了相同的介面。 3. 開 ...
  • [比賽鏈接](http://codeforces.com/contest/952) 感覺可能比洛谷的題目正常一點 就當學英語了吧 A Quirky Quantifiers比較正常,不是什麼很坑的題目。 判斷奇偶性即可 Code: B A Map of the Cat 給你兩種貓的被膜的感受,判斷是哪 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...