Why does my JAR file not create a serialization?

試著忘記壹切 提交于 2019-12-25 01:23:54

问题


I created a jar file from a Java project. I can even run it now, but it does not save the serialization file that is stored correctly when I run the program from normal binary code outside of the JAR file.

Can’t you create new files when you have a jar file?

This is how I write the file:

FileOutputStream fileOut = new FileOutputStream("data/vocabulary.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this);
out.close();
fileOut.close();

My JAR file looks like this:

META-INF
    MANIFEST.MF
name
    stefankoch
        …

where name/stefankoch/… is my project namespace.

I tried creating a data directory in the jar-file (root), but it did not work either. There also is a data directory within the directory the jar-file resides in, but that one is not taken either.

How can I allow jar files to create files?


回答1:


basically, you should not add information entered by the user into a JAR file, they belong to the user’s home directory.

This can be read with

System.getProperty("user.home")

And files within the user home directory can also be used from within a JAR file.



来源:https://stackoverflow.com/questions/11350212/why-does-my-jar-file-not-create-a-serialization

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!