0

Getting an error when trying to open a FileInputStream to load Map from file with .ser extension.

Constructor where I create new File and invoke method that loads map from file:

protected DriveatorImpl() {
    accounts = new ConcurrentHashMap<String, Client>();
    db = new File("database.ser"); // oddly this does not create a file if one does not exist
    loadDB(); 
}

@SuppressWarnings("unchecked")
private void loadDB() {
    try {
        fileIn = new FileInputStream(db);
        in = new ObjectInputStream(fileIn);
        accounts = (Map<String, Client>) in.readObject();
        in.close();
        fileIn.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

I've tried to create file manually and put it in same package with class, but it does not help. What's going on?!

Thank You!

GarRudo
  • 158
  • 1
  • 10
  • *// oddly this does not create a file if one does not exist* - not really, why do you think it should? – Scary Wombat Jan 13 '17 at 06:22
  • Oh, I am missing db.createNewFile(); – GarRudo Jan 13 '17 at 06:33
  • What would be the point of that? If the file doesn't exist, you'd rather know that than get a different exception because it was empty. – user207421 Jan 13 '17 at 06:53
  • In real development scenario it makes sense what you are talking about. This is a college project, I am creating empty file in case program will run fresh on some other machine. Thanks for the tip. – GarRudo Jan 13 '17 at 07:04
  • Doesn't answer the question. If the file doesn't exist, you'd rather know that, rather than getting a different problem because the file is empty. If you need to create it *with some content*, do that. `File.createNewFile()` isn't a universal bandaid. – user207421 Jan 13 '17 at 07:06
  • The idea application carries is that I want to load into map every time I run Server. When it is a fresh app run, by default file is empty anyways because there was no entries made into map and hence nothing was saved to it in first place. I know this is not the way to maintain database, but project doesn't specifically tell that we have to implement proper database. But I see your point and most likely I will have problems at fresh run. Any suggestions how I can do things differently so that ObjectInputStream never be constructed with empty stream? – GarRudo Jan 13 '17 at 07:23
  • When it is a fresh application you need to initialize the file with default or empty content, via something like `new ObjectOutputStream(new FileOutputStream(file)).close()`. Not just create an empty file. Otherwise you get these exceptions. – user207421 Jan 13 '17 at 09:24
  • @EJP Sorry, but "something like new ObjectOutputStream(new FileOutputStream(file)).close()" did not fix an error and exception still comes out. But what I noticed is that file is not empty anymore, it has a size of 4 bytes. – GarRudo Jan 13 '17 at 10:08

2 Answers2

0

You provide a relative path for the file. That means program will look for the file relative to the working directory.

Depending on how you run the program it will be the directory you run it from (if run from Shell/Cmd) or whatever is configured in the project settings (if run from the IDE). For the latter, it depends on the IDE but usually it's the project root directory.

More info on working directory: https://en.wikipedia.org/wiki/Working_directory
More info on relative path: https://en.wikipedia.org/wiki/Path_(computing)#Absolute_and_relative_paths

Regarding creation of the file, it would create non-existing file if you were to write to it. When you read it, it expects it to exist. That means you have to create empty file (if one does not exist) before reading or simply treat exception as empty content.

Pijusn
  • 11,025
  • 7
  • 57
  • 76
  • Still can't figure out how to configure it. My working directory would be Bank-Services. Path to a class where I need resource is Bank-Services\src\ie\gmit\sw\server\database – GarRudo Jan 13 '17 at 06:52
  • @GarRudo I think you might want to Google "read java resources". Resources (unlike source files) are bundled into the JAR. Please note that resources don't change once bundled. – Pijusn Jan 13 '17 at 07:03
  • @Pijusn I would need that as well as I plan to make a JAR for Server side to run on some other machine. I though resource would be any peripheral document or hardware that is required by application to do what it meant to do with it. Am I wrong? – GarRudo Jan 13 '17 at 07:08
  • @GarRudo Not sure what you mean by _hardware_ but resources are just files (any type) that are bundled with the JAR. Common use-cases: templates, data, configs, images. Essentially, if it's a file - you can put it as a resource. Your application may depend on more than resources (and usually does). Such as hardware, external configuration files, databases, ... These, however, can only exist outside your JAR and be checked/accessed at runtime. – Pijusn Jan 13 '17 at 07:26
  • I definitely want to keep this database.sir external to a JAR, because I want to modify it. It "acts" as a database in this project. I have to carry it around with JAR of a server if I want to run it on different machine. – GarRudo Jan 13 '17 at 07:36
0
  • The path to the file you have given might be wrong for IDE it can take relative path but from the command line, it will take the absolute path.
Community
  • 1
  • 1
Gopinath Langote
  • 311
  • 4
  • 10
  • And if the file doesn't exist it will create an empty one, which will just cause a different problem while masking the real problem. – user207421 Jan 13 '17 at 06:55
  • as per the answer i have suggested , i clearly said that, if you want to create empty file than getting file not found exception. So if you don't want to.go by this way just ignore and said other solution also. I doubt how it will cause new problem by creating empty file? – Gopinath Langote Jan 13 '17 at 07:00
  • In other words you've never tried it. `new ObjectInputStream(...)` will throw an `EOFException` if constructed with an empty stream. – user207421 Jan 13 '17 at 07:02
  • I agree on that point. looks like only relative and absolute path issue. i will update my solution – Gopinath Langote Jan 13 '17 at 07:06
  • By the way, that is exactly an error I am receiving now after fixing path to a file. :) java.io.EOFException – GarRudo Jan 13 '17 at 07:42
  • hello, now your error is different first it was FileNotFound Exception now it's EOFException . which means file read exception but in your question you poated as FileNotFound Exception. – Gopinath Langote Jan 13 '17 at 07:44
  • So, fundamentally you don't want to help further because it is a post about different error? – GarRudo Jan 13 '17 at 07:57
  • no. I will update my answer soon. i suggest you also to update question with the current error that you are getting and the code that you have updated. thank you. – Gopinath Langote Jan 13 '17 at 07:59
  • I think I would have to create separate post with a problem, because updating existing one would make answers unrelated to new problem and I would have to reconsider accepted answer. – GarRudo Jan 13 '17 at 08:09
  • sure. you can. create question and if you want me to try answering it. let me know new question url – Gopinath Langote Jan 13 '17 at 08:11
  • Here is a link to a new post http://stackoverflow.com/questions/41630365/workaround-java-io-eofexception-cause-by-objectinputstream. Thank you for cooperation. – GarRudo Jan 13 '17 at 08:41