Java - 序列化 - 抓取对象数量

时间:2014-07-29 13:11:54

标签: java serialization

Java - 序列化 - 抓取文件中的对象数


我正在尝试从序列化文件中检索我的对象,然后将它们重新添加到我的文件中。似乎有一个问题,没有异常被抛出但在运行我的方法时我的控制台中没有打印任何内容。在继续这里之前是我的代码:

public boolean openCollection(){
        try {
            FileInputStream e = new FileInputStream("profiles.ser");
            ObjectInputStream inputStream = new ObjectInputStream(e);
            List<Profile> profiles = (List<Profile>) inputStream.readObject();

            //De-obscure
            for(Profile p : profiles){
                String unObcName = deobscure(p.getName()); //Original name
                String unObcSurname = deobscure(p.getSurname()); //Original surname
                String unObcUsername = deobscure(p.getUsername()); //Original username
                String unObcPassword = deobscure(p.getPassword()); //Original password

                p.setName(unObcName);
                p.setSurname(unObcSurname);
                p.setUsername(unObcUsername);
                p.setPassword(unObcPassword);

                //Debugging
                System.out.println("DE-OBSCURE - Profile name: " + p.getName() +"\n"+
                        "Profile surname: " + p.getSurname() +"\n"+
                        "Profile username: " + p.getUsername() +"\n"+
                        "Profile password: " + p.getPassword());
                this.profiles.add(p);
            }

        } catch (FileNotFoundException var3) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JOptionPane.showMessageDialog(null, "No profiles found, please create a profile!");
                    final CreateProfile createProfile = new CreateProfile();
                    createProfile.setVisible(true);
                }
            });
            return false;
        } catch (IOException var4) {
            var4.printStackTrace();
            JOptionPane.showMessageDialog(null, "IO Exception");
            return false;
        } catch (ClassNotFoundException var5) {
            var5.printStackTrace();
            JOptionPane.showMessageDialog(null, "Required class not found");
            return false;
        }
    return true;
}

  

这是序列化方法

 public void saveCollection(){

    //Obscure the data
    List<Profile> saveProfiles = new ArrayList<>();
    for(Profile p : profiles){
        String obcName = obscure(p.getName());
        String obcSurname = obscure(p.getSurname());
        String obcUsername = obscure(p.getUsername());
        String obcPassword = obscure(p.getPassword());

        p.setName(obcName);
        p.setSurname(obcSurname);
        p.setUsername(obcUsername);
        p.setPassword(obcPassword);

        //Debugging
        System.out.println("DEBUG - Profile name: " + p.getName() + "\n" +
                "Profile surname: " + p.getSurname() + "\n" +
                "Profile username: " + p.getUsername() + "\n" +
                "Profile password: " + p.getPassword());

        saveProfiles.add(p);
    }

    //Save it
    try {
        FileOutputStream e = new FileOutputStream("profiles.ser");
        ObjectOutputStream outputStream = new ObjectOutputStream(e);
        outputStream.writeObject(saveProfiles);
        outputStream.flush();
        outputStream.close();
    } catch (IOException var3) {
        var3.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error. Cannot save database.");
    }

}

创建配置文件时,正确地隐藏了详细信息,结果如下:

  

DEBUG - 个人资料名称:OBF:1u2a1toa1w8v1tok1u30

     

简介姓氏:OBF:1u2a1toa1w8v1tok1u30

     

个人资料用户名:OBF:1u2a1toa1w8v1tok1u30

     

个人资料密码:OBF:1u2a1toa1w8v1tok1u30

然而,当运行openCollection()时,没有任何内容被打印到控制台中。

注意:个人资料详细信息均为“admin”,这就是所有数据看起来相同的原因

1 个答案:

答案 0 :(得分:0)

经过一段时间检查后,我发现这个问题只是我的deobscuring方法。我现在已对其进行了编辑,并将其替换为以下内容:

/**
 * @param str Obscured String
 * @return Unobscured String
 */
private String deobscure(String s){
    if (s.startsWith(__OBFUSCATE)) s = s.substring(4);

    byte[] b = new byte[s.length() / 2];
    int l = 0;
    for (int i = 0; i < s.length(); i += 4)
    {
        if (s.charAt(i)=='U')
        {
            i++;
            String x = s.substring(i, i + 4);
            int i0 = Integer.parseInt(x, 36);
            byte bx = (byte)(i0>>8);
            b[l++] = bx;

        }
        else
        {
            String x = s.substring(i, i + 4);
            int i0 = Integer.parseInt(x, 36);
            int i1 = (i0 / 256);
            int i2 = (i0 % 256);
            byte bx = (byte) ((i1 + i2 - 254) / 2);
            b[l++] = bx;
        }
    }

    return new String(b, 0, l,StandardCharsets.UTF_8);
}
  

原始个人资料名称:&#34;管理员&#34;

     

隐藏的个人资料名称:&#34; OBF:1npu1toa1w8v1tok1nsc

     

取消隐藏个人资料名称后:&#34;管理员&#34;

因此,该方法现在按预期工作。