ObjectOutputStream。java
ObjectOutputStream fw= new數組 ObjectOutputStream( new FileOutputStream("F:\\aaa\\a.txt"));ide |
接口的類的對象均可以被序列化。若是沒有實現此接口,那麼這個類的對象就不能學習
被序列化或者反序列化,不然會拋出 NotSerializableException。測試
註明是瞬態的,使用 transient 關鍵字修飾,則這個屬性就不能被序列化。this
public class Student implements Serializable {編碼 private static final long serialVersionUID = 727935469565319794L;idea private String name;spa private String address;.net private Integer age; private Integer score;
public Student(){
}
public Student(String name, String address, Integer age, Integer score) { this.name = name; this.address = address; this.age = age; this.score = score; }
public static long getSerialVersionUID() { return serialVersionUID; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
public Integer getScore() { return score; }
public void setScore(Integer score) { this.score = score; }
public String toString() { return "Student{" + "name='" + name + '\'' + ", address='" + address + '\'' + ", age=" + age + ", score=" + score + '}'; } }
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream;
//序列化父類java.io.OutputStream 子類:FileOutputStream // 序列化:java.io.ObjectOutputStream繼承了OutputStream類 public class Meth04 { public static void main(String[] args) {
//構造方法 ObjectOutputStream(OutputStream out) //建立一個寫入指定的OutputStream的ObjectOutputStream。
//建立序列化對象 //ObjectOutputStream fw=new ObjectOutputStream(new FileOutputStream("F:\\aaa\\a.txt"));
//建立序化的對象 Student student=new Student("王智雅","山西市",20,99); Student student1=new Student("李文傑","天水市",23,98); Student student2=new Student("桑鳳嬌","泰安市",24,99); Student student3=new Student("郭朝旭","聊城市",25,96); Student student4=new Student("陳天意","承德市",27,100);
try(ObjectOutputStream fw=new ObjectOutputStream(new FileOutputStream("F:\\aaa\\a.txt"));){ fw.writeObject(student); fw.writeObject(student1); fw.writeObject(student2); fw.writeObject(student3); fw.writeObject(student4);
}catch (FileNotFoundException ce ){ ce.printStackTrace(); }catch (IOException ce){ ce.printStackTrace(); } } |
ObjectInputStream (InputStream in):建立從指的InputStream 讀取的 ObjectInputStream。
//反序列化父類 java.io.InputStream 子類時FileinputStream //java.io.ObjectInputStream 繼承了InputStream
//反序列化示例: public class Meth05 { public static void main(String[] args) { //建立反序列化的對象 // ObjectInputStream object=new //ObjectInputStream(new FileInputStream("F:\\aaa\\a.txt"));
//建立Student 對象 Student student=new Student(); //讀文件 try(ObjectInputStream object=new ObjectInputStream(new FileInputStream("F:\\aaa\\a.txt"));){ Student ss=(Student)object.readObject(); while(ss!=null){ System.out.println(ss); ss=(Student)object.readObject(); } }catch(ClassNotFoundException ce){ ce.printStackTrace(); }catch (IOException ce){
} } } /* 運行的結果: Student{name='王智雅', address='山西市', age=20, score=99} Student{name='李文傑', address='天水市', age=23, score=98} Student{name='桑鳳嬌', address='泰安市', age=24, score=99} Student{name='郭朝旭', address='聊城市', age=25, score=96} Student{name='陳天意', address='承德市', age=27, score=100}*/ |
當 JVM 反序列化對象時,拋出了一個 InvalidClassException 異常,發生這個異常的緣由以下:
一、 該類的序列化版本號與從流中讀取的類描述的版本不匹配
二、 該類包含未知數據類型Serializable 接口給須要序列化的類,提供了一個序列化版本號,serialVersionUID 該版本號的目的在於驗證序列化的對象和對應類是否版本匹配。
上一章咱們學習了字節流和字符流,接下來咱們學習一些更強大的流,好比:可以高效讀寫
的緩衝流,可以轉換編碼的轉換流,可以持久化存儲對象的序列化流等待。這些功能更爲強
大的流,都是在基本流對象基礎之上建立而來的,這些功能更強大的流,至關因而對基本流
對象的一種加強。
緩衝流的基本原理: 是在建立流對象時,會建立一個內置的默認大小的緩衝區數組,經過緩衝區讀寫,(往內存中讀寫數據是最塊的),減小系統IO的次數,從而提升讀寫的效率。
//普通的流實現數據的傳送:消耗的時間:16193毫秒 public class Meth06 { public static void main(String[] args) { //開始時間 long start=System.currentTimeMillis(); try(FileInputStream input=new FileInputStream("E:\\idea64.exe"); FileOutputStream output=new FileOutputStream("F:\\copy.exe");){ int i; while ((i=input.read())!=-1){ output.write(i); }
}catch (IOException ce){ ce.printStackTrace(); } long end=System.currentTimeMillis();
System.out.println("複製普通文件的消耗的時間:"+(end-start)+"毫秒"); } } |
// BufferedStream字節緩衝流實現傳送:消耗的時間:94毫秒 public class Meth07 { public static void main(String[] args) { //開始時間 long start=System.currentTimeMillis(); try(BufferedInputStream fr=new BufferedInputStream(new FileInputStream("E:\\idea64.exe")); BufferedOutputStream fw=new BufferedOutputStream(new FileOutputStream("F:\\copy1.exe"))){ int i; while ((i=fr.read())!=-1){ fw.write(i); }
}catch (IOException ce){ ce.printStackTrace(); } //結時間 long end=System.currentTimeMillis(); System.out.println("複製普通文件的消耗的時間:"+(end-start)+"毫秒"); } } |
// BufferedStream字節緩衝流用數組實現實現複製,更快 複製普通文件的消耗的時間:16毫秒 public class Meth08 { public static void main(String[] args) { //開始時間 long start=System.currentTimeMillis(); try(BufferedInputStream fr=new BufferedInputStream(new FileInputStream("E:\\idea64.exe")); BufferedOutputStream fw=new BufferedOutputStream(new FileOutputStream("F:\\copy2.exe"))){ int i; //使用數組 byte[]bytes=new byte[1024*8]; while ((i=fr.read(bytes))!=-1){ fw.write(bytes,0,i); }
}catch (IOException ce){ ce.printStackTrace(); } long end=System.currentTimeMillis(); System.out.println("複製普通文件的消耗的時間:"+(end-start)+"毫秒"); } } |
1 字符緩衝流:BufferedReader 讀
//BufferedReader 字符緩衝流讀操做 public class Meth09 { public static void main(String[] args)throws IOException{
BufferedReader fr=new BufferedReader(new FileReader("F:\\aaa\\a.txt")); String s=null; //每次讀取一行的內容 while ((s=fr.readLine())!=null){ System.out.println(s); } fr.close(); } } |
2 字符緩衝流: BufferedWriter 寫
//BufferedWriter 字符緩衝流 寫 public class Meth10 { public static void main(String[] args)throws IOException{ BufferedWriter fw=new BufferedWriter(new FileWriter("F:\\aaa\\a.txt")); fw.write(65); //實現換行 fw.newLine(); fw.write("李文傑我愛個人祖國"); fw.newLine(); fw.write("hgsahgd"); fw.newLine(); fw.close(); } } |
1計算機中儲存的信息都是用二進制表示的,而咱們在屏幕上看到的數字,英文,標點符號、
漢字等字符是二進制數轉換以後的結果。將字符存儲到計算機中,稱爲編碼。反之,將存儲在計算機中的二進制數按照某種規則解析出來,稱爲解碼。好比說,按照 A 規 則存儲,一樣按照 A 規則解析,那麼就能顯示正確的文本符號。反之,按照 A 規則存儲,再
按照 B 規則解析,就會致使亂碼現象。
2 字符編碼(Character Encoding)就是一套天然語言的字符與二進制數之間的對應規則。
127 的字符連在一塊兒就表示一個漢字,這樣大約能夠組合了包含 7000 多個簡體漢字。
共收錄 20000 多個漢字,兼容 GB2312。
//轉換流 InputStreamReader,將字節轉換爲字符,實現讀取的操做 public class Meth11 { public static void main(String[] args)throws IOException{ //指定字符的編UF碼 InputStreamReader input=new InputStreamReader(new FileInputStream("F:\\aaa\\a.txt"),"UTF-8"); int i; while ((i=input.read())!=-1){ System.out.print((char)i); } input.close(); } } |
//轉換流 OutputStreamWriter 將字符轉換爲字節,實現存儲的操做(寫) public class Meth12 { public static void main(String[] args)throws IOException{ //指定字符的編碼 OutputStreamWriter fr=new OutputStreamWriter(new FileOutputStream("F:\\aaa\\a.txt"),"UTF-8"); fr.write("李文傑@1314520"); fr.write("sangfenjirjie"); fr.write("liwnejie"); fr.close(); } } |
//打印流,只有輸出流,沒有輸入流 public class Meth13 { public static void main(String[] args)throws IOException{ //static void setOut(PrintStream out);打印流 PrintStream print=new PrintStream("F:\\aaa\\a.txt"); System.setOut(print); System.out.println("liwnejie"); System.out.println("sangfengjiao"); System.out.println("wangzhiys"); } } |