2

Currently I'm trying to make a level editor for a Java game that I created.
My problem is that my program doesn't save the edited levels.

Here is the code that I made so far:

Path path = Paths.get(gameFile.getAbsolutePath());
FileSystem fs = FileSystems.newFileSystem(path, null);
Path p = fs.getPath("rpg"+fs.getSeparator()+"levels"+fs.getSeparator()+"level"+(short)level+".png");
OutputStream os = Files.newOutputStream(p);
ImageIO.write(img, "png", os);
os.close();
fs.close();

gameFile is the .jar file of my game which contains the levels and the levels are saved as .png files.
I have tried to save my picture outside of the jar and it worked so i guess i made a mistake with the FileSystem or the Path.
The Path in the jar is rpg/levels/level*.png

Edit:
I changed

OutputStream os = Files.newOutputStream(p);

to

OutputStream os = Files.newOutputStream(p,StandardOpenOption.CREATE,StandardOpenOption.TRUNCATE_EXISTING);

but now I get an error when closing the fileSystem

java.nio.file.FileSystemException: G:\RPG.jar: The process cannot access the file because it is being used by another process.

at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.implDelete(Unknown Source)
at sun.nio.fs.AbstractFileSystemProvider.delete(Unknown Source)
at java.nio.file.Files.delete(Unknown Source)
at com.sun.nio.zipfs.ZipFileSystem.sync(ZipFileSystem.java:1294)
at com.sun.nio.zipfs.ZipFileSystem.close(ZipFileSystem.java:277)
at de.jorolf.editor.LevelEditor$5.actionPerformed(LevelEditor.java:213)
Hack-R
  • 22,422
  • 14
  • 75
  • 131
Jorolf
  • 50
  • 8
  • If an user answered your question please also **accept** his answer ([Accepting Answers: How does it work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)). If not than please specify what remains unanswered, this is a really crucial part of StackOverflow, thank you very much. – Zabuzard Jul 27 '17 at 12:27

1 Answers1

1

You can not save anything inside of a running jar-File (related: updating-a-jar-whilst-running).

Also, if you want to save something inside of a not running jar-File, then you also need to save the file compressed. For this task you can simply add a ZipOutputStream to your stream-queue (Java-Doc: ZipOutputStream.html). Also java.util.jar.JarFile and java.util.jar.JarEntry may be helpful.

For your general task, you should save the level file outside of the jar. You may distribute the game in a folder game with /game/theGame.jar and /game/firstLevel.lvl. Then the level is outside of the jar and you can modify it.

Also you may use common directories as other games do, like Windows Documents or AppData for game specific settings and data.

MyDocuments in Java: FileSystemView.getFileSystemView().getDefaultDirectory().getPath() AppData in Java: System.getenv("APPDATA")

Community
  • 1
  • 1
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • If I would save my levels in MyDocuments or AppData it would be bound to a specific computer, but I want the game to be like 'plug & play'. I tried using ZipOutputStream but it deletes all my files that were previously in my jar file – Jorolf Jul 10 '16 at 20:12
  • You can extract the whole archive via `ZipInputStream`, modify and then recreate the archive with `ZipOutputStream`, just google around *java how to modify a jar*. But I'd recommend to use the first option. Use a folder **"MyGame"** which contains the `jar` and resource folders. Then you are able to fully control the resources as they are outside of the jar itself. – Zabuzard Jul 10 '16 at 20:47
  • As said before, the class `JarFile` may be very helpful for changing the contents of a `jar-File`. Here's the doc: [JarFile.html](https://docs.oracle.com/javase/7/docs/api/java/util/jar/JarFile.html) – Zabuzard Jul 10 '16 at 20:54
  • I would use JarFile but i dont see how i could add files to that. I can get an entry, but if it doesnt exist I just an an error. – Jorolf Jul 11 '16 at 08:50
  • Here's some code to add files to a jar-File: http://stackoverflow.com/a/1281295/2411243 – Zabuzard Jul 11 '16 at 10:09
  • My problem is that the `JarOutputStream` doesn't contain the files that are in the jar File. I tried to add them by making a `JarFile` and getting all the entries by going trough a `Enumeration` but I get a `java.util.zip.ZipError` when calling the function `nextElement()`. – Jorolf Jul 11 '16 at 16:42
  • You may create a new question for this problem. It appears to be too complex for solving it in the comment section and it differs from the initial question/problem :) – Zabuzard Jul 11 '16 at 16:56