ObjectInputStream和ObjectOutputStream读取写入边界判定

1.ObjectOutputStreamModel.java 负责生成对象文件
package com.kevinb.io.objectStream;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import com.kevinb.io.bean.User;

public class ObjectOutputStreamModel {

    private static void writeObjectFile(){
        String sourceFile = "src/com/kevinb/io/objectStream/userFile";
        File file = new File(sourceFile);
        try {
            FileOutputStream fiOutputStream = new FileOutputStream(file);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fiOutputStream);
            //开始准备插入对象
            User user1 = new User();
            user1.setUserId(1);
            user1.setUserName("Kevinb");
            user1.setPassword("ffffff");
            objectOutputStream.writeObject(user1);
            
            User user2 = new User();
            user2.setUserId(2);
            user2.setUserName("Jone");
            user2.setPassword("11");
            objectOutputStream.writeObject(user2);
            
            //写入结束标志方便读取(非常重要,如果不写入,在读取的时候无法定位读取结束);
            objectOutputStream.writeObject(null);
            //close outputStream
            objectOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("对象写入成功!");
    }
    
    public static void main(String[] args) {
        writeObjectFile();
    }
}

2.ObjectInputStreamModel.java  负责解析对象文件
package com.kevinb.io.objectStream;

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

import com.kevinb.io.bean.User;

public class ObjectInputStreamModel {

    @SuppressWarnings("unchecked")
    private static void readObjectFile(){
        String sourcePath = "src/com/kevinb/io/objectStream/userFile";
        File file = new File(sourcePath);
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            /**
             * 读取方法:采用while语句进行读取,但是在写这个文件的时候一定要添加一个结束标志,否则
             * 使用下面的代码会产生错误;
             */
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            Object object;
            //读取结束标志位:是再次读取一个对象,判定对象是否是空,如果不为null继续读取,如果为null停止读取;
            while((object=objectInputStream.readObject()) != null){
                User user = (User)object;
                System.out.println(user.getUserId() + " "+ user.getUserName()+" "+user.getPassword());
            }
            
            //关闭对象输入流
            objectInputStream.close();
            //关闭文件输入流
            fileInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        readObjectFile();
    }

}



转自:http://baizewen.blog.sohu.com/103162370.html

你可能感兴趣的:(String,object,user,File,null,Class)