楼主到网上搜一下,有很多例子的,一看就知道了,比如你到javaeye网站上搜一下看看。

解决方案 »

  1.   

    话说数组里面是object??还是基本元素?
    写了最简单的版本,你参照下,其实自己也没写过Serializable的程序,当是练手了
    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 java.io.Serializable;
    public class SerializableTry implements Serializable 
    {
            int a;
            int b;
            public SerializableTry()
            {this(0,0);}
            public SerializableTry(int a,int b){
             this.a=a;
             this.b=b;
            }
            public void setA(int newa){
             a=newa;
            }
            public void setB(int newb){
             b=newb;
            }
            public int getA()
            {
             return a;
            }
            public int getB(){
             return b;
            }
            public static void main(String[] args)
            {
             SerializableTry st=new SerializableTry(1,2);
             FileOutputStream fos;
             FileInputStream fi;
    try {
    //#--------------------------------store the oject
    fos = new FileOutputStream("a.txt");
             ObjectOutputStream os = new ObjectOutputStream(fos);
             os.writeObject(st);
             os.close();
             fos.close();
             //-----------------------------------read the object
             fi=new FileInputStream("a.txt");
             ObjectInputStream ins=new ObjectInputStream(fi);
             st=(SerializableTry)ins.readObject();
             System.out.println(st.getA()+st.getB());
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }        
            }
    }
      

  2.   


    用的SQLServer数据库,表名:arrayTest,表结构是:
    id -----int(整形)
    array---image(在SQlServer中的二进制类型,它存放的内容是一个数组)
    package edu.sdkd;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import edu.sdkd.dataSource.JdbcUtils;//这是我自己的一个连接数据库的类
    public class SerialTest {
        public static String[] read(int id)throws Exception{//从数据库读出来
            String[] retVal=null;
         String sql="select * from arrayTest where id =?";
         Connection conn=JdbcUtils.getConnection();
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs=ps.executeQuery();
            if(rs.next()){
             InputStream in=rs.getBinaryStream("array");
             ObjectInputStream oin=new ObjectInputStream(in);
             retVal=(String[])oin.readObject();
             in.close();
             oin.close();
            }
            JdbcUtils.free(rs, ps, conn);
            return retVal;
        }
        public static void write(int id,String[] arr)throws Exception{//写入数据库
         String sql="insert into arrayTest values(?,?)";
    Connection conn=JdbcUtils.getConnection();
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setInt(1, id);
            
            ByteArrayOutputStream bos=new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bos);   
            oo.writeObject(arr);  
            byte[] buff=bos.toByteArray();
            ByteArrayInputStream bis=new ByteArrayInputStream(buff);
            ps.setBinaryStream(2, bis,buff.length);
            
            ps.executeUpdate();
            oo.close();
            bis.close();
            JdbcUtils.free(null, ps, conn); 
        }
    public static void main(String[] args)throws Exception {
    String arr[]={"做个测试","玩一玩","helloworld"};
    int id=1;
    write(id,arr);
    System.out.println(java.util.Arrays.toString(read(id)));
    }
    }
      

  3.   

    点击JAVA序列化
      

  4.   

    除了上面的链接
    还有别人写的序列化的工具类
    public class SerializableEntity {    /**
        * String path  序列化出的实体文件保存路径
        * Object o     需要序列化的对象
        * return void
        */
        public static void writeEntity(String path, Object o){
            FileOutputStream fileOut = null;
            ObjectOutputStream objOut = null;
            
            try {
                fileOut = new FileOutputStream(path+"/DBObj.obj",true);
                objOut = new ObjectOutputStream(fileOut);
                objOut.writeObject(o);
                objOut.flush();
                objOut.close();
                fileOut.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        /**
        * String path  序列化出的实体文件路径
        * return Object
        */
        public static Object readEntity(String path){
            Object o = null;
            try {
                FileInputStream fileIn = new FileInputStream(path);
                ObjectInputStream objIn = new ObjectInputStream(fileIn);
                o = objIn.readObject();
                objIn.close();
                fileIn.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            
            return o;
        }
    }
      

  5.   

    实现序列化的关键: 
    1,需要序列化的类实现Serializable接口 
    2,建立一个OutputStream(可以是文件输出,也可以是别的输出)作为ObjectOutputStream的参数 
    用ObjectOutputStream的writeObject(Object)方法输出对象 
    3,用ObjectInputStream的readObject()方法读出对象 
    package csdn;import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.ObjectOutputStream;
    import java.io.ObjectInputStream;
    import java.io.Serializable;public class ClassSerialize {
        public static void main(String args[]) {
            myObj obj1 = new myObj("jordan", "america", "[email protected]");
            try {
                ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
                ObjectOutputStream objOut = new ObjectOutputStream(byteOut);
                objOut.writeObject(obj1);            ObjectInputStream objIn = new ObjectInputStream(
                        new ByteArrayInputStream(byteOut.toByteArray()));
                myObj obj = (myObj) objIn.readObject();            System.out.println("name:"+obj.getName());
                System.out.println("address:"+obj.getAddress());
                System.out.println("mail:"+obj.getMail());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }class myObj implements Serializable {
        private String name;    private String address;    private String mail;    public myObj(String name, String address, String mail) {
            this.name = name;
            this.address = address;
            this.mail = mail;
        }
        
        public String getName(){
            return this.name;
        }
        public String getAddress(){
            return this.address;
        }
        public String getMail(){
            return this.mail;
        }
    }
      

  6.   

    google 一下。网上挺多的。不知道lz 有什么想法。
      

  7.   

    package com.yebol.realtimesearcher.test;import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.sql.Blob;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.Random;
    import java.sql.Connection;public class User implements Serializable {
    private int id;
    private String name;
    private String password; public int getId() {
    return id;
    } public void setId(int id) {
    this.id = id;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public String getPassword() {
    return password;
    } public void setPassword(String password) {
    this.password = password;
    } public User() { }

    public String toString(){
    return "id:" + getId() + "," + getName() + "," + getPassword();
    }
    public static void main(String args[]) throws Exception { Class.forName("com.mysql.jdbc.Driver").newInstance();
    String uri = "jdbc:mysql://localhost/test?user=root&password=&useUnicode=true&characterEncoding=utf-8";
    Connection con = DriverManager.getConnection(uri);
    PreparedStatement stmt = con.prepareStatement("insert into user (data) values (?)");
    stmt.setBlob(1, getData());
    stmt.executeUpdate();
    stmt.close();
    //query
    stmt = con.prepareStatement("select data from user");
    ResultSet rs = stmt.executeQuery();
    printResult(rs);
    stmt.close();
    con.close();
    } private static void printResult(ResultSet rs)  throws Exception{
    while(rs.next()){
    Blob data = rs.getBlob("data");
    ObjectInputStream in = new ObjectInputStream(data.getBinaryStream());
    User u = (User)in.readObject();
    //print user
    System.out.println(u);
    }

    } private static InputStream getData() throws Exception{
    User u = new User();
    Random random = new Random();
    u.setId(random.nextInt());
    u.setName("name:" + random.nextInt());
    u.setPassword("password:" + random.nextInt());

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bout);
    out.writeObject(u);
    out.close();
    return new ByteArrayInputStream(bout.toByteArray());
    }

    }
    输出结果:
    id:-2111737040,name:-2132504785,password:-587804072
    id:-1574799089,name:-689238908,password:1768480760
      

  8.   

    非常感谢大家的参与,cangyingzhijia   bobo_916(一杯花茶) goldenfish1919
     
    (若鱼) nihuajie05
     
    (陆烨辰) 感谢你们,我想跟大家说,我要做的是把数组序列化到MY数据库中,然后再从数据库中取出来。还有,能不能关健地方加上注释呀
      

  9.   

    对了,我要用hiberate,不要用 jdbc