IO流(五)其他流

節點流

ByteArrayInputStream

public class ByteArrayInputStream extends InputStream

ByteArrayInputStream 包含一個內部緩衝區,該緩衝區包含從流中讀取的字節。

構造方法

ByteArrayInputStream(byte[] buf) //創建一個ByteArrayInputStream,使用buf作爲緩衝區數組。
ByteArrayInputStream(byte[] buf,int offset,int length)  //創建ByteArrayInputStream,使用buf的一部分作爲緩衝區數組。

沒有新增方法,繼承自InputStream的方法。

ByteArrayOutputStream

public class ByteArrayOutputStreamextends OutputStream

此類實現了一個輸出流,其中的數據被寫入一個byte數組。緩衝區會隨着數據的不斷寫入而自動增長。可使用toByteArray()和toString()獲取數據。

構造方法

ByteArrayOutputStream()  //創建一個新的byte數組輸出流
ByteArrayOutputStream(int size)  //創建一個新的byte數組輸容量出流,它具有指定大小的緩衝區

Demo

public class TestByteArray{
    public static void main(String[] args) throws IOException{
        read(write());
    }
    public static void read(byte[] b) throws IOException{
        InputStream is = new BufferedInputStream(new ByteArrayInputStream(b));
        byte[] flush = new byte[1024];
        int len = 0;
        while(-1 != is.read(flush)){
            System.out.println(new String(flush));
        }
        is.close();
    }
    //輸出流有新增方法,不能使用多態
    public static byte[] write() throws IOException{
        byte[] dest;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        String msg = "操作與文件輸入流一致";
        byte[] info = msg.getBytes();
        bos.write(info,0,info.length);
        dest = bos.toByteArray();
        bos.close();
        return dest;
    }
}

處理流

DataInputStream

public class DataInputStream extends FileterInputStream implements DataInput

數據輸入流允許應用程序以機器無關方式從底層輸入流中讀取基本Java數據類型。

構造方法

DataInputStream(InputStream in) //使用指定的底層InputStream 創建一個DataInputStream

DataOutputStream

public class DataOutputStream extends FilterOutputStream implements DataOutput

數據輸出流允許應用程序以適當方式將基本Java數據類型寫入輸出流中。然後,應用程序可以使用數據輸入流將數據讀入。

Demo

public class TestData{
    public static void main(String[] args) throws IOException{
       try {
        read("D:/io/a.txt");
    } catch (Exception e) {
        e.printStackTrace();
        }
    }
    public static void read(String destPath){
        File src = new File(destPath);
        DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(src)));
        double num1 = dis.readDouble();
        long num2 = dis.readLong();
        String str = dis.readUTF();
        System.out.println(str);
    }
    public static void write(String destPath){
        double point = 2.5;
        long num = 100L;
        String src = "數據類型";
        File dest = new File(destPath);
        DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));
        dos.writeDouble(point);
        dos.writeLong(num);
        dos.writeUTF(src);
        dos.flush();
        dos.close();
    }
}

實現序列化和反序列化

ObjectOutputStream

序列化

public class ObjectOutputStream extends OutputStream implements ObjectOutput,ObjectStreamConstants

可以使用ObejctOutputStream重構對象。通過在流中使用文件可以實現對象的持久存儲。

注意:

  • 先序列化後反序列化;反序列化順序必須與序列化一致

Demo

    import java.io.Serializable;
    /**
     * 實現序列化接口後該類可以被序列化
     */
    public class Emp implements Serializable{
        /**
         * 版本號
         */
        private static final long serialVersionUID = 1L;
        private String name;
        private int age;
        private String gender;
        private double salary;
        public Emp(String name, int age, String gender, double salary) {
            this.name = name;
            this.age = age;
            this.gender = gender;
            this.salary = salary;
        }
        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 String getGender() {
            return gender;
        }
        public void setGender(String gender) {
            this.gender = gender;
        }
        public double getSalary() {
            return salary;
        }
        public void setSalary(double salary) {
            this.salary = salary;
        }
        @Override
        public String toString() {
            return "Emp [name=" + name + ", age=" + age + ", gender=" + gender
                    + ", salary=" + salary + "]";
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }
    }

將Emp對象序列化到文件emp.obj中
- 使用屬性name,age,gender,salary爲[“張三”,15,”男”]
- 創建文件字節輸出流FileOutputStream類的對象,接着使用該文件字節輸出流對象作爲參數構造對象字節輸出流ObjectOutputStream類的對象
- 使用ObjectOutputStream類的writeObject方法將Emp對象寫入到文件emp.obj中
- 關閉oos對象流,以釋放資源

public class TestOisAndOos {
    /**
     * 使用OOS實現對象的序列化
     */
    @Test
    public void testOOS()throws Exception{
        FileOutputStream fos = new FileOutputStream("emp.obj");
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        Emp emp = new Emp("張三",15,"男",4000);
        oos.writeObject(emp);
        System.out.println("序列化完畢");
        oos.close();        
    }
}

ObjectInputStream

實現對Emp對象的反序列化
- 創建文件字節輸入流FileInputStream類的對象,接着使用該文件輸入流對象作爲參數構造對象字節輸入流ObjectInputStream類的對象
- 使用ObejctInputStream類的readObject方法將Emp對象從emp.obj文件中讀取出來
- 關閉ois對象,以釋放資源

public class TestOisAndOos {
    /**
     * 使用OOS實現對象的序列化
     */
    @Test
    public void testOOS()throws Exception{
        //...(代碼略)
    }
    /**
     * 使用OIS實現對象的反序列化
     */
    @Test
    public void testOIS()throws Exception{
        FileInputStream fis = new FileInputStream("emp.obj");
        ObjectInputStream ois = new ObjectInputStream(fis);

        Emp emp = (Emp)ois.readObject();
        System.out.println("反序列化完畢");
        System.out.println(emp);
        ois.close();        
    }
}
發佈了35 篇原創文章 · 獲贊 30 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章