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?