0

Suppose, I have made a class named Worker.

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.

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. 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? As doing so will take a lot of time. Thanks in advance.

  • 2
    The answer is: no. --- Solving problems like this is why *databases* were invented. – Andreas Jan 18 '21 at 10:26
  • Aww man. I was hoping someone would tell me I wasn't paying attention and there was some method I could use. – Redeemer X Jan 18 '21 at 10:28
  • 1
    See e.g. [Which embedded DB written in Java for a simple key/value store?](https://stackoverflow.com/q/9772058/5221149) – Andreas Jan 18 '21 at 10:33
  • As a note, [you should not be using the Java serialization mechanism at all](https://www.infoworld.com/article/3275924/oracle-plans-to-dump-risky-java-serialization.html). – daniu Jan 18 '21 at 10:49
  • @daniu should I just start using database then as others are saying? I am just a university student working on a completely beginner project so right now I can manage with this manual search. But are you saying that database is the better alternative? – Redeemer X Jan 18 '21 at 11:05
  • @RedeemerX Is this for an assignment or a personal learning project? – daniu Jan 18 '21 at 11:37
  • @daniu this is an assigned project, also I am kinda out of time. I will have to write the entire thing within tonight (JavaFx). But either way if you'd give me some pointers like others were kind enough to give regarding this, it will definitely help me in future projects (Which I plan to take time and do properly :P ) – Redeemer X Jan 18 '21 at 13:13

2 Answers2

0

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.

Danon
  • 2,771
  • 27
  • 37
0

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.

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. It's best to route all Worker management (adding, removing, querying, etc) through this class so it can stay up to date. 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.

Also, passwords should never be stored as Strings; that's a security issue due to how Strings are stored in Java; it may linger in memory even after you're done using it.

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.

daniu
  • 14,137
  • 4
  • 32
  • 53