java读取arrayList<Integer> - objectInputStream EOFEXception

问题描述

嗨,我正在尝试读取 Integer 的 ArrayList 并获得 EOFException。

我有一个问题的 ArrayList,它是序列化的,我做同样的事情来读取它没问题,但是 ArrayList Integer 不起作用。

我像这样写两个ArrayList:(我省略了所有其他不相关的字段)

    String sqlQuery = "insert into test values (?,?,?)";
    
            PreparedStatement pst = null;
            try {
    
                if (DBConnector.myConn != null) {
                    pst = DBConnector.myConn.prepareStatement(sqlQuery);
    
                    // serialize object
                    Blob questionsBlob = DBConnector.myConn.createBlob();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(baos);
                    oos.writeObject(t.getQuestions());
                    oos.close();
    
                    Blob pointsBlob = DBConnector.myConn.createBlob();
                    ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
                    ObjectOutputStream oos2 = new ObjectOutputStream(baos2);
                    oos.writeObject(t.getPointsPerQuestion());
                    oos2.close();
    
                    // store in byte array
                    byte[] questionsAsByte = baos.toByteArray();
                    byte[] pointsAsByte = baos2.toByteArray();
    
                    // fill blob object with byte array
                    questionsBlob.setBytes(1,questionsAsByte);
                    pointsBlob.setBytes(1,pointsAsByte);
                    pst.setBlob(3,questionsBlob);
                    pst.setBlob(4,pointsBlob);
                    pst.executeUpdate();

并读取对象:

String sqlQuery = "select * from test where teacherUsername = \"" + username + "\";";
        ArrayList<Test> arr = new ArrayList<Test>();
        ArrayList<Question> questions;
        ArrayList<Integer> points;
        try {
            if (DBConnector.myConn != null) {
                Statement st = DBConnector.myConn.createStatement();
                ResultSet rs = st.executeQuery(sqlQuery);
                while (rs.next()) {
                    questions = new ArrayList<>();
                    points = new ArrayList<>();
                    
                    Blob questionsBlob = rs.getBlob(3);
                    BufferedInputStream bis = new BufferedInputStream(questionsBlob.getBinaryStream());
                    ObjectInputStream ois = new ObjectInputStream(bis);
                    questions = (ArrayList<Question>) ois.readobject();
                    System.out.println(questions);
                    
                    Blob qPointsBlob = rs.getBlob(4);
                    BufferedInputStream bis1 = new BufferedInputStream(qPointsBlob.getBinaryStream());
                    ObjectInputStream ois1 = new ObjectInputStream(bis1);
                    try {
                    points = (ArrayList<Integer>) ois1.readobject(); // PROBLEM HERE
                    }catch(EOFException e) {
                        System.out.println(points);
                        
                    }

当我尝试读取数据时 - 它与 Question 的 ArrayList 一起工作并真正向我展示问题,但使用 Integer 的 ArrayList 它给了我 EOFException。 有什么想法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)