1

I have a files folder with MO.data that I want to save an object to.

  try{
     FileOutputStream t = new FileOutputStream(String.valueOf(GameReader.class.getResourceAsStream("MO.data")));
     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(t));

        out.writeObject(map);
        out.close();
        System.out.println("File saved and closed!");
        return true;

    }catch(IOException ioe){
        System.err.print(ioe);
        return false;
    }

I have: src/files/GameReader.class and src/files/MO.data

My class I'm working from is located in src/logic/saver.class

However, it keeps saving it to a file: java.io.BufferedInputStream@11b1857c in src/java.io.BufferedInputStream@11b1857c

What am I doing wrong?

user207421
  • 305,947
  • 44
  • 307
  • 483
Ryan Sangha
  • 442
  • 6
  • 17
  • 3
    FileOutputStream takes a `String` file name/location. You're writing your data to the reference name of the getResourceAs. – Compass May 14 '18 at 18:55
  • Ohh, I see. I've been struggling with this for the past hour, haven't found anything on here that works, at least for me. I can't give a full path either because I'm building a Jar later as well. `FileOutputStream t = new FileOutputStream("files/MO.data");`didn't work either. This is the path I get when I right click the file and press "get relative path" in intellij – Ryan Sangha May 14 '18 at 19:03
  • You can use the File/Files/Path libs built into Java. File will create one relative to working directory if you don't provide a full path, and the other two can facilitate that. – Compass May 14 '18 at 19:20
  • Correct me if I'm wrong, but won't using File/Files/Path give errors when building to a Jar file? – Ryan Sangha May 14 '18 at 19:26
  • Is the goal to write it into a physical directory, or the jar? If it's the physical directory where the jar is being run, then you only need a file name. – Compass May 14 '18 at 19:30
  • I want to write it to the MO.data file in the files folder, which would be in the jar, right? – Ryan Sangha May 14 '18 at 19:39
  • 1
    You should not be writing to files inside the jar. Files that may change in content should exist outside of the jar. – Compass May 14 '18 at 20:19
  • Alright, so just one last thing, you've been really helpful. Since this works and I'm really tired of trying to put it in the damn folder, should I just let it stay in the folder where the Jar file is created? Or maybe put in a "home folder", like https://stackoverflow.com/a/15940030/9706444 ? – Ryan Sangha May 14 '18 at 20:32
  • It depends on what level of seriousness you want. The best practice is that distribution should stay immutable and files it works with (configs, logs, sessions) should be elsewhere. Otherwise shared distributions, containerizations and other things might not work. – Imaskar May 15 '18 at 05:46
  • You *cannot* write files inside the JAR, in general. Some platforms may permit it: some do not. @Compass – user207421 May 15 '18 at 08:40

1 Answers1

0

You're constructing an output stream, then take its string presentation and somehow assume it is a valid path. Instead get a URI of this resource which will provide you with an absolute path of it.

URL url = GameReader.class.getResource("MO.data");
String.valueOf(url.getPath()+url.getFile());

EDIT:

I'm sorry I missed that you're trying to write to the bundled resource. It will be read-only, and you have to put it into resources/ folder, not src/. If you want to store some session data, you may want to use temp folder System.getProperty("java.io.tmpdir") or user home System.getProperty("user.home") or current working directory (don't add any path then). Then you add file name to this path and save there. Sometimes a hybrid approach is needed, when you read a saved template bundled within a jar and then save modified data into another place.

Imaskar
  • 2,773
  • 24
  • 35
  • FileOutputStream t = new FileOutputStream(String.valueOf(GameReader.class.getResource("MO.data").getPath())); didn't work. Can't call getAbsolutePath() on GameReader.class.getResource("MO.data") – Ryan Sangha May 14 '18 at 19:25
  • Im sorry, don't have an IDE at hand. Try url.getPath()+url.getFile() – Imaskar May 14 '18 at 19:29