简体   繁体   English

如何从文件中获取序列化 object 的特定实例?

[英]How to get a particular instance of serialized object from file?

Suppose, I have made a class named Worker.假设,我制作了一个名为 Worker 的 class。

public class Worker implements Serializable{
    String username, password;
} 

And say, in the main function/any other method that works with this class, I created few instances.并且说,在与此 class 一起使用的主要功能/任何其他方法中,我创建了几个实例。

Worker first = new Worker("Arty", "Sif");  //Worker(String name, String pass)
Worker second = new Worker("Sirius", "Pass");

And then I stored them in a file, for example "store.bin" using objectoutputstream.然后我将它们存储在一个文件中,例如使用 objectoutputstream 的“store.bin”。 My question is, if I want a particular instance during runtime, Sirius for example, is there any OTHER way to get it than just reading through the entire file loading each object and comparing them with a unique field of the instance?我的问题是,如果我在运行时想要一个特定的实例,例如 Sirius,除了读取加载每个 object 的整个文件并将它们与实例的唯一字段进行比较之外,还有其他方法可以获得它吗? As doing so will take a lot of time.因为这样做会花费很多时间。 Thanks in advance.提前致谢。

Well, there are already designed tools for that and they are databases.好吧,已经有为此设计的工具,它们是数据库。 They do exactly what you described.他们完全按照你的描述做。

There's even one called SQLite, which does even the implementation as you described, storing data as a binary file, say sqlite.db .甚至还有一个叫做 SQLite,它甚至可以按照您的描述执行实现,将数据存储为二进制文件,例如sqlite.db

What you seem to be doing is read the file every time someone asks for a specific worker;您似乎在做的是每次有人要求特定工作人员时读取文件; you shouldn't need to do that.你不应该这样做。 Just read the file once and put all results in a Map .只需读取一次文件并将所有结果放入Map中。

class PasswordLoader {
  Map<String, String> users = new HashMap<>();
  public WorkerManager(String filename) {
    List<Worker> all = readFromFile(filename);
    for (Worker worker : all) {
      users.put(worker.getName(), worker.getPassword());
    }
  }

  public String getPasswordForUsername(String username) {
    return users.get(username);
  }
}

It does have several issues;它确实有几个问题; for example, if new Workers get added, the file will be updated but the map won't.例如,如果添加了新的工人,文件将被更新,但 map 不会。 It's best to route all Worker management (adding, removing, querying, etc) through this class so it can stay up to date.最好通过此 class 路由所有 Worker 管理(添加、删除、查询等),以便它可以保持最新状态。 Another advantage of doing it this way is that if you want to change how the data is stored later (eg. using a database), you only need to change this one class because it's the only one in contact with how the Workers are stored.这样做的另一个好处是,如果您想更改以后的数据存储方式(例如,使用数据库),您只需要更改这个 class 因为它是唯一与工人存储方式相关的。

Also, passwords should never be stored as String s;此外,密码永远不应该存储为String that's a security issue due to how Strings are stored in Java;由于字符串如何存储在 Java 中,这是一个安全问题; it may linger in memory even after you're done using it.即使在您完成使用后,它也可能会在 memory 中徘徊。

As I wrote in my comment, it's not a good idea to use Java Serialization either, but since you've got that down, pressed for time and it's not a production project, I guess it'll do.正如我在评论中所写,使用 Java 序列化也不是一个好主意,但既然你已经把它弄下来了,时间紧迫,而且它不是一个生产项目,我想它会做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM