• JAVA序列化和反序列化 对象<=>IO流 对象<=>字节数组


    http://developer.51cto.com/art/201202/317181.htm

    http://blog.csdn.net/earbao/article/details/46914407

    package com.cmy.demo;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    
    import com.cmy.serial.Student;
    
    public class UserStudent {
    
        /**
         * @param args
         * @throws IOException 
         * @throws ClassNotFoundException 
         */
        public static void main(String[] args) throws IOException, ClassNotFoundException {
            
            Student st = new Student("Tom",'M',20,3.6); 
            File file = new File("C:\YiKiChen\pacho\student.txt");
            try {
                file.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
            //序列化 对象转换成io流
            try {
                FileOutputStream fos = new FileOutputStream(file);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(st);
                oos.flush();
                oos.close();
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            // 反序列 io转换成对象
            try {
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
                Student st1 = (Student)ois.readObject();
                System.out.println(st1.getName());
                System.out.println(st1.getGpa());
                System.out.println(st1.getSex());
                
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
         /**  
         * 对象转数组  
         * @param obj  
         * @return  
         */  
        public byte[] toByteArray (Object obj) {      
            byte[] bytes = null;      
            ByteArrayOutputStream bos = new ByteArrayOutputStream();      
            try {        
                ObjectOutputStream oos = new ObjectOutputStream(bos);         
                oos.writeObject(obj);        
                oos.flush();         
                bytes = bos.toByteArray ();      
                oos.close();         
                bos.close();        
            } catch (IOException ex) {        
                ex.printStackTrace();   
            }      
            return bytes;    
        }   
           
        /**  
         * 数组转对象  
         * @param bytes  
         * @return  
         */  
        public Object toObject (byte[] bytes) {      
            Object obj = null;      
            try {        
                ByteArrayInputStream bis = new ByteArrayInputStream (bytes);        
                ObjectInputStream ois = new ObjectInputStream (bis);        
                obj = ois.readObject();      
                ois.close();   
                bis.close();   
            } catch (IOException ex) {        
                ex.printStackTrace();   
            } catch (ClassNotFoundException ex) {        
                ex.printStackTrace();   
            }      
            return obj;    
        }   
    
    
    }
  • 相关阅读:
    Spoj-ANTP Mr. Ant & His Problem
    Spoj-BIPCSMR16 Team Building
    cf575A Fibonotci
    cf660E Different Subsets For All Tuples
    cf468C Hack it!
    cf468B Two Sets
    cf701E Connecting Universities
    Spoj-BLMIRINA Archery Training
    Spoj-BITDIFF Bit Difference
    Spoj-VISIBLEBOX Decreasing Number of Visible Box
  • 原文地址:https://www.cnblogs.com/tingbogiu/p/6211958.html
Copyright © 2020-2023  润新知