java - I / O - Object stream

ObjectInputStream

ObjectOutputStream

 

package io;

import java.io.*;

public class ObjectStreamTest {
    public static void main(String[] args){

        TargetObject to1 = new TargetObject("aaa");  //测试用对象

        FileInputStream fis = null;
        FileOutputStream fos = null;

        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        try {
            fos = newA FileOutputStream ( "C: \\ \\ the Users Test Administrator \\ \\ \\ objectTest.txt Desktop" ); 
            OOS = new new the ObjectOutputStream (fos);   // advanced stream, for processing low-level flow, stream objects, an array of flow, cache flow are all high-level flow 
            oos.writeObject (TO1); 
            oos.flush (); 
            // into success, but the content is garbled, not posted 


            FIS = new new FileInputStream ( "C: \\ \\ the Users Administrator \ \ Desktop \\ \\ objectTest.txt Test " ); 
            OIS = new new the ObjectInputStream (FIS); 
            the TargetObject TO2 = (the TargetObject) ois.readObject ();   // the readObject () returns the type Object, a strong need to turn 
            to2.show ();
            //name = aaa| num = 1
            //读取成功


        }catch(Exception e){
            e.printStackTrace();
        }finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

Test category

Package Penalty for IO; 

Import the java.io.Serializable; 

public  class TargetObject the implements Serializable {   // After inheriting must be serialized interface to serialize (broken down into fixed format) saved in a file 
    Private String name;
     Private  static  int NUM; 

    public TargetObject () { 
    } 

    public the TargetObject (String name) {
         the this .name = name; 
        NUM ++ ; 
    } 

    public String getName () {
         return name; 
    } 

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

    public static int getNum() {
        return num;
    }

    public void show(){
        System.out.print("name = " + name + "| num = " + num);
    }
}

 

 

Low flow:

FileInputStream

FileOutputStream

FileWriter

FileReader

 

Advanced Stream:

Advanced Stream is a work by low-level flow, it can be understood as an extension of the lower stream

such as:

缓存流  BufferedReader, BufferedWriter, BufferedInputStream, BufferedOutputStream

String flow StringReader, StringWriter

数组流  ByteArrayInputStream ByteArrayOutputStream, CharArrayReader, CharArrayWriter

Object flow ObjectInputStream, ObjectOutputStream

Guess you like

Origin www.cnblogs.com/clamp7724/p/11645619.html