0

For a school project, i realized a program that simulates the archive of library.

One of my classes, FileManager.java, located in \src\controller\, reads from/writes in three files, containing the informations i need; for read/write I use a FileInputStream/FileOutputStream wrapped with an ObjectInputStream/ObjectOutputStream, here is the code that writes data from the model into files, named archive.users and archive.items.

public void writeObjectIntoFile(final String fileName, final Model model) {
    try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(this.path + fileName))) {
            if (fileName.contains("users")) {
                oos.writeObject(model.getUserArchive());
            } else if (fileName.contains("items")) {
                oos.writeObject(model.getItemArchive());
            } 
        }
}

The variabile this.path is a String initialized with the path to the archive files, which are located in \res\, the variable is initialized this way:

private String path = System.getProperty("user.dir") + System.getProperty("file.separator") + "res"
        + System.getProperty("file.separator");

And the class with the method main, called Application.java, which starts the program, is located in the folder \src\controller\.

My problem comes when i export the program into a .jar file, because it doesn't find my files anymore, and i cannot read the content of files to initialize the library archive or save new entries that i create with my GUI.

The problem is with the path or with the Streams?

Enrico Casanova
  • 51
  • 2
  • 10
  • Make sure you start your program in the correct directory. See: http://stackoverflow.com/questions/16239130/java-user-dir-property-what-exactly-does-it-mean – Bob Brinks Sep 06 '16 at 14:55
  • The jar should work in the same way in every folder, because the archive files are inside it, it just doesn't find them – Enrico Casanova Sep 07 '16 at 14:20
  • Ah then you should be loading them as resources: http://stackoverflow.com/questions/3861989/preferred-way-of-loading-resources-in-java – Bob Brinks Sep 07 '16 at 14:21

0 Answers0