FileOutputStream,BufferedOutputStream,對象的序列化與反序列化ObjectOutputStream

(1)FileOutputStream:節點流,向文件寫出字節的流

package day0825io;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 節點流:低級流
 * 
 * 處理流:高級流
 * 1:不能獨立存在,構造方法通常會傳入另一個流
 * 2:用於處理另一個流
 * 3:使用高級流的目的是爲了簡化讀寫操作
 * 
 * OutputStream 字節輸出流的父類
 */
public class FosDemo1 {

    public static void main(String[] args) throws IOException {
        //FileOutputStream:節點流,向文件寫出字節的流
        FileOutputStream fos = new FileOutputStream("fos.dat");
        fos.write(97);
        String str = "認真而不執着";
        byte[] buf = str.getBytes("UTF-8");
        fos.write(buf);
        fos.close();
    }

}

(2)使用文件字節流對文件寫操作


public class FosDemo2 {

    public static void main(String[] args) throws IOException {
        /**
         * FileOutputStream支持一個重載的構造方法
         * FileOutputStream(String str,boolean append)
         * 第2個參數指定是否進行追加操作
         * 若不追加,當前文件中所有內容都會被清除,然後寫數據
         * 
         * 注意這一點跟RandonAccessFile不同
         */
        FileOutputStream fos = new FileOutputStream("fos.dat",true);
        fos.write(99);
        fos.close();

    }

}

(3)FileInputStream:節點流,用於從文件中讀取字節的流

package day0825io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

//FileInputStream:節點流,用於從文件中讀取字節的流------
public class FisDemo3 {
/**
 * fos.dat文件中的內容(utf-8編碼,共20個字節):
 * a認真而不執着c
 */
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("fos.dat");
        int i = fis.read();//讀取一個字節
        System.out.println(i);//97

        byte[] buf = new byte[40];
        int len = fis.read(buf);//實際讀取到的字節量
        System.out.println(len);//19

        String str = new String(buf,"UTF-8");//讀取到的buf轉爲String
        System.out.println(str);//認真而不執着c
    }

}

BufferedOutputStream

package day0825io;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class BosDemo4 {

    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("bos.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        String str = "東方紅太陽升";
        byte[] buf = str.getBytes("UTF-8");
        bos.write(buf);
        /**
         * flush()
         * 強制將當前緩衝區的緩衝數據全部寫出
         * 無論緩衝區是否被裝滿
         */
        //bos.flush();
        bos.close();//先flush,然後關閉內部流
    }

}

(5)用輸入輸出流實現文件的複製

package day0825io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

//(5)用輸入輸出流實現文件的複製----
public class FisFosCopy5 {

    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("src.jpg");
        FileOutputStream fos = new FileOutputStream("copy.jpg");

        /*
        int d = -1;
        while((d=fis.read())!=-1){
            fos.write(d);
        }
        */

        byte[] buf = new byte[10240];//10K
        int len = -1;
        while((len=fis.read(buf))!=-1){////len:實際讀取到的字節量
            fos.write(buf);
        }

        fis.close();
        fos.close();
    }

}

(6)緩衝流(高級流):加快讀寫時間

package day0825io;
//(6)緩衝流(高級流):加快讀寫時間------
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class BisBosCopy6 {

    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("src.jpg");
        BufferedInputStream bis = new BufferedInputStream(fis);

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.jpg"));

        int d = -1;
        while((d=bis.read())!=-1){
            //讀取一個字節,當讀完整個數組以後,再去讀一組數據回來
            bos.write(d);//只拿一個字節,當拿滿數組時,一次性寫出去
        }
        //java只關閉最外層的流即可,bis內部自己關閉裏面的流
        bis.close();
        bos.close();
    }

}

Person類

package day0825io;

import java.io.Serializable;
import java.util.List;
/**
 * 一個對象若想通過ObjectOutputStream進行序列化
 * 那麼該對象所屬的類必須實現Serializable接口
 * 
 * 該接口沒有任何抽象方法
 * 實現該接口僅僅用於標識當前類的實例可以被序列化
 */
public class Person implements Serializable{
    /**
     * 當類的屬性增加或者修改了,若版本號不變
     * 那麼反序列化時儘量兼容現有版本
     * 若版本號發生了改變,那麼反序列化時會拋出異常
     */
    private static final long serialVersionUID = 1L;
    private String name;
    private int age;
    //transient僅僅用於序列化及反序列化時被忽略
    private transient char gender;
    //private int salary;
    private double other;
    private List<String> otherInfo;

    public Person() {}

    public Person(String name, int age, char gender, double other,
            List<String> otherInfo) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        //this.salary = salary;
        this.other = other;
        this.otherInfo = otherInfo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

//  public int getSalary() {
//      return salary;
//  }
//
//  public void setSalary(int salary) {
//      this.salary = salary;
//  }

    public double getOther() {
        return other;
    }

    public void setOther(double other) {
        this.other = other;
    }

    public List<String> getOtherInfo() {
        return otherInfo;
    }

    public void setOtherInfo(List<String> otherInfo) {
        this.otherInfo = otherInfo;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", gender=" + gender
                +",other:"+other+  ", otherInfo=" + otherInfo + "]";
    }


}

(7)將對象序列化

package day0825io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
 * 1:序列化:將特定的數據結構轉換爲一組字節的過程
 * 2:持久化:數據寫入硬盤長久保存的過程
 * 
 * 序列化與反序列化一般用於:
 * 1:傳輸
 * 2:保存
 */
public class TestPerson7 {

    public static void main(String[] args) throws IOException {
        List<String> list = new ArrayList<String>();
        list.add("上海");
        list.add("漢族");
        list.add("其他信息");

        Person p = new Person("張三",22,'男',2000,list);

        FileOutputStream fos = new FileOutputStream("p.obj");
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        /**
         * void writeObject(Object o)
         * ObjectOutputStream提供的方法
         * 可以將給定的對象轉換爲一組字節後寫出
         */
        oos.writeObject(p);
        oos.close();
    }

}

(8)將存儲對象的文件 反序列化

package day0825io;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class OisDemo8 {
    /**
     * ObjectInputStream是一個高級流
     * 可以將一組字節轉換爲對應的對象,用於對象的反序列化
     * @param args
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        FileInputStream fis = new FileInputStream("p.obj");
        ObjectInputStream ois = new ObjectInputStream(fis);

        Person p = (Person)ois.readObject();
        System.out.println(p.toString());
        ois.close();
    }

}
發佈了0 篇原創文章 · 獲贊 7 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章