日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

转换流/序列化/反序列化

發布時間:2025/3/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 转换流/序列化/反序列化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉換流:

使用轉換流可以在一定程度上避免亂碼,還可以指定輸入輸出所使用的字符集
InputStreamReader:是從字節流到字符流的橋梁,父類是Reader
OutputStreamWriter:是從字符流到字節流的橋梁,父類是Writer

轉換流圖解:

轉換流的構造方法:

方法名說明
InputStreamReader(InputStream in)使用默認字符編碼創建InputStreamReader對象
InputStreamReader(InputStream in,String chatset)使用指定的字符編碼創建InputStreamReader對象
OutputStreamWriter(OutputStream out)使用默認字符編碼創建OutputStreamWriter對象
OutputStreamWriter(OutputStream out,String charset)使用指定的字符編碼創建OutputStreamWriter對象
public static void main(String[] args) throws IOException {// JDK11前的方法OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("/Users/itzhuzhu/Desktop/Java/ideaTest/io/b.txt"));InputStreamReader isr = new InputStreamReader(new FileInputStream("/Users/itzhuzhu/Desktop/Java/ideaTest/io/a.txt"), "UTF-8");// JDK11后子字符流推出了新的構造方法FileReader fr = new FileReader("/Users/itzhuzhu/Desktop/Java/ideaTest/io/a.txt", Charset.forName("gbk"));char[] chars = new char[1024];int len;while ((len = isr.read(chars)) != -1) {osw.write(chars, 0, len);}osw.close();isr.close();}
對象操作流:

對象序列化:

將Java中的對象保存到文件中

  • 對象序列化:就是將對象保存到磁盤中,或者在網絡中傳輸對象
  • 這種機制就是使用一個字節序列表示一個對象,該字節序列包含:對象的類型、對象的數據和對象中存儲的屬性等信息
  • 字節序列寫到文件之后,相當于文件中持久保存了一個對象的信息
  • 反之,該字節序列還可以從文件中讀取回來,重構對象,對它進行反序列化

構造方法

方法名說明
ObjectOutputStream(OutputStream out)創建一個寫入指定的OutputStream的ObjectOutputStream

序列化對象的方法

方法名說明
void writeObject(Object obj)將指定的對象寫入ObjectOutputStream

對象反序列化:

將文件中的對象,加載到程序中

構造方法

方法名說明
ObjectInputStream(InputStream in)創建從指定的InputStream讀取的ObjectInputStream

反序列化對象的方法

方法名說明
Object readObject()從ObjectInputStream讀取一個對象

serialVersionUID:

序列化了一個對象后修改了對象再去讀就會異常(InvalidClassException)
解決:給對象所屬的類加一個serialVersionUID ,值隨便定,不超Long就行

transient:

如果一個對象中的某個成員變量的值不想被序列化,就給成員變量加transient關鍵字修飾,該關鍵字標記的成員變量不參與序列化過程
static: 被這個關鍵字修飾的成員變量在序列化時不會保存到文件中

-1&null的使用場景:

while ((len = bis.read(bytes)) != -1) { bos.write(bytes, 0, len); }

read():讀取到文件末尾返回值是 ‐1

readLine():讀取到文件的末尾返回值 null

readObject():讀取到文件的末尾 直接拋出異常(把序列化的對象存儲到集合中,把集合序列化到文件中)

代碼演示:

// 把某個對象序列化就要把對象實現Serializable接口,但是不需要重寫,因為Serializable沒有方法,就是標記性接口 public class Student implements Serializable {public static final long serialVersionUID = 1L;private String username;private transient String password;public Student() {}public Student(String username, String password) {this.username = username;this.password = password;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return "Student{" +"username='" + username + '\'' +", password='" + password + '\'' +'}';} }

測試類:

public class Demo02 {public static void main(String[] args) throws IOException, ClassNotFoundException {// write();// read();readArray();}//反序列化private static void read() throws IOException, ClassNotFoundException {ObjectInputStream ois = new ObjectInputStream(new FileInputStream("/Users/itzhuzhu/Desktop/Java/ideaTest/io/b.txt"));Object obj = ois.readObject();Student s = (Student) obj;System.out.println(s);ois.close();}//序列化private static void write() throws IOException {ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/Users/itzhuzhu/Desktop/Java/ideaTest/io/b.txt"));Student s = new Student("itzhuzhu", "12345");oos.writeObject(s);oos.close();}//反序列化讀取多個對象private static void readArray() throws IOException, ClassNotFoundException {Student s1 = new Student("韓信", "hanxin");Student s2 = new Student("李白", "libai");Student s3 = new Student("露娜", "luna");ArrayList<Student> list = new ArrayList<>();list.add(s1);list.add(s2);list.add(s3);// 創建序列化對象ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/Users/itzhuzhu/Desktop/Java/ideaTest/io/b.txt"));// 把對象裝到集合再存到文件oos.writeObject(list);oos.close();// 創建反序列化對象ObjectInputStream ois = new ObjectInputStream(new FileInputStream("/Users/itzhuzhu/Desktop/Java/ideaTest/io/b.txt"));// 把文件里的對象數據讀取到內存ArrayList<Student> list2 = (ArrayList<Student>) ois.readObject();for (Student student : list2) {System.out.println(student);}ois.close();} }
打印流:

打印流分類:

字節打印流: PrintStream 字符打印流: PrintWriter

打印流的特點:

只有輸出方向 讓內容原樣輸出

構造方法:

  • PrintStream?(String fileName) 通過路徑名字符串創建打印流
  • PrintStream?(File file) 通過File對象創建打印流
  • PrintStream?(OutputStream out) 通過OutputStream創建打印流

普通方法:

  • void print?(Xxx x) 打印后不換行
  • void println?(Xxx x) 打印后換行
public static void main(String[] args) throws FileNotFoundException {// test01();// out是System類中的一個靜態成員變量,什么類型呢?PrintStream字節打印流// 我們可以修改out的流向System.out.println(65);System.out.println("你好");// 自己創建一個新的字節打印流PrintStream ps = new PrintStream("day10demo\\abc\\oo.txt");System.setOut(ps);System.out.println("97");System.out.println("大家好");}private static void test01() throws FileNotFoundException {// 創建打印流PrintStream ps = new PrintStream("day10demo\\abc\\ps.txt");// 打印數據ps.println(110);ps.println(36.0d);ps.println('好');ps.println(true);ps.println("真的好");// 關閉流ps.close();}

總結

以上是生活随笔為你收集整理的转换流/序列化/反序列化的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。