如何在序列化中查找下一个数组

时间:2016-08-17 15:51:48

标签: java arrays serialization keyboard

我的代码出错了为什么我无法读取第二个数组。

布局:

public class Layout implements Serializable {

    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public Layout(){
        username="";
        password="";
    }
    public Layout(String user,String pass){
        username=user;
        password=pass;
    }
    @Override
    public String toString() {
        return "Match Found! [Username: " + username + ", Password: " + password+"]";
    }
}

由于我的布局正常工作,我尝试序列化用户输入并反序列化。

序列化:

int i;
Layout[] items = new Layout[1];

CrunchifySerializeDeserialize c = new CrunchifySerializeDeserialize();
for (i = 0; i < items.length; i++) {

    items[i] = c.new Layout();
}

String a = JOptionPane.showInputDialog("Enter Username");
String b = JOptionPane.showInputDialog("Enter Password");
items[0].setUsername(a);
items[0].setPassword(b);

System.out.println("Personal info Details.....");
for (Layout d : items) {
    System.out.print(d.getUsername());
    System.out.print("\t" + d.getPassword());
}

List<Layout> obj;
obj = new ArrayList<Layout>();

for (i = 0; i < items.length; i++) {
    obj.add(items[i]);
}
items=new Layout[1];

try {

    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("./Test1.txt",true));
    out.writeObject(obj);

    out.close();

    System.out.println("\nSerialization Successful... Checkout your specified output file..\n");

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

在反序列化中,我认为我在阅读时出错了吗?如果我错了xD * RIP ME,请纠正我。

try {
        //FileInputStream fileIn = new FileInputStream("./Crunchify_Test1.txt");
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("./Test1.txt"));
        boolean check = true;
        while(check){
            try{
            System.out.println("Deserialized Data: \n" + in.readObject().toString());
            //reinitailize shit

            }catch(EOFException e){
                check=false;
            }
        }
        //System.out.println("Deserialized Data: \n" + in.readObject().toString());

        in.close();
        //fileIn.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

在txt文件中添加时出现错误,显示“无效的类型代码:AC”,这意味着它无法读取该*堆中的其他来源。

0 个答案:

没有答案