twitter4j serializable正在保存但未加载

时间:2017-03-26 06:40:54

标签: java serialization deserialization twitter4j

我希望保存并加载我的twitter feed,首先将其保存到tweet.ser文件中,然后使用load()方法检索它。但我得到一个错误:“线程中的异常”主“java.lang.ClassCastException:twitter4j.ResponseListImpl无法强制转换为[Ltwitter4j.Status;     在com.teamtreehouse.RealTreets.load(RealTreets.java:25)     在Example.main(Example.java:12)“

这是我的代码:

import java.io.*;
import java.util.List;
import twitter4j.Status;

public class RealTreets{
    public static void save(List<Status> status){
        try(
            FileOutputStream fos = new FileOutputStream("tweets.ser");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            oos.writeObject(status);
        }catch(IOException ioe) {
            System.out.println("Problem saving Tweets");
            ioe.printStackTrace();
        }
    }
    public static Status[] load(){
        Status[] status = new Status[0];
        try(
            FileInputStream fis = new FileInputStream("tweets.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
        ){
            status = (Status[]) ois.readObject();
        } catch(IOException ioe){
            System.out.println("Error reading file!");
            ioe.printStackTrace();
        } catch(ClassNotFoundException cnfe){
            System.out.println("Error loading treets.");
            cnfe.printStackTrace();
        }
        return status;
    }
}

我已经检查了...保存工作正常。 tweets.ser正在显示数据。 load()方法中有一个issus。

1 个答案:

答案 0 :(得分:1)

您正在将 Scenario: Authentication Given I am on the homepage And I press "Login" button Then I should see "Login Successfully" 保存到文件中,但正在尝试将其作为List<Status>读取。这些都不是一回事,因而失败了。

您需要更改保存方式或阅读方式,以便彼此保持一致。