-2

I cannot figure out a way to write data into a .dat file without overwriting already existing data. This is what I have so far:

public static void fileData(List<? extends Loggable> l, String pathName) throws IOException {
        var file = new File(pathName);
        var fileOut = new FileOutputStream(file);
        
        var objOut = new ObjectOutputStream(fileOut);
        objOut.close();
        fileOut.close();
    }
  • 2
    @tgdavies The OP is not writing a text file, and doing this with an `ObjectOutputStream` doesn't work AFAIK, as it will result in multiple object stream headers in the file. – Mark Rotteveel Sep 11 '21 at 10:43
  • If you want to do this, then either you need to have a single stream for the entire duration of your program (if you need to store persistent state across program invocations, then you will need to read it first and write it back). However, using `ObjectOutputStream` is probably not the right tool for this. You may want to use a database instead. – Mark Rotteveel Sep 11 '21 at 10:44
  • 1
    It would help to give us a little bit more context about the purpose of the `.dat` files. What consumes them? How often are they updated? How large do they get? – tgdavies Sep 11 '21 at 23:56
  • Is this your actual code? You're never using `l` or have shown any `.dat`, so why does extension matter? – OneCricketeer Sep 12 '21 at 03:10
  • 1
    @tgdavies The .dat files are supposed to store objects that implement the Loggable interface. Of course each different type will be saved to a different file. They will probably be updated every month or so or more depending on the object. In terms of how large they will get, I'm not exactly sure as to what and how many objects are considered "large." But I feel the main issue here is if I can save data and read from files without overwriting them and without any errors. Then I can solve other issues such as proper data management and usage of .dat files. – ultracookies Sep 14 '21 at 03:24
  • @OneCricketeer Currently it is my code but is empty of implementation. I am not using `l` because I had not found out a way to properly write data to the `.dat` file without overwriting existing data at the time. My main issue now is trying to read that same data from the `.dat` file without getting any exceptions. – ultracookies Sep 14 '21 at 03:30
  • Doesn't that only happen when you create and use more than `ObjectOutputStream` object? Also how would I store create a single stream for the entire duration of my program? – ultracookies Sep 14 '21 at 03:34
  • I don't understand. Please edit your question to include the actual exceptions you're getting and the code that generates them – OneCricketeer Sep 14 '21 at 12:56

1 Answers1

2

FileOutputStream constructor has a second optional parameter that can be set to true to open file for appending instead of overwriting.

var fileOut = new FileOutputStream(file, true);

Any data written in this mode will be added to the end of the file, leaving any original data intact.

Lev M.
  • 6,088
  • 1
  • 10
  • 23
  • I'm trying your solution but am running into some problems that I haven't ran into before when using the ObjectInputStream class. I first kept getting StreamCorruptedException and now I am getting EOFException. – ultracookies Sep 10 '21 at 23:35
  • 1
    @Rigel Why are you using `ObjectInputStream` on `FileOutputStream`? Are you trying to read from a file you opened for writing? You need to show your real code if you want help with your actual problem! There is no `ObjectInputStream` in the code you posted. – Lev M. Sep 10 '21 at 23:59
  • 1
    If you write twice to an object stream this will happen: https://stackoverflow.com/questions/2393179/streamcorruptedexception-invalid-type-code-ac – tgdavies Sep 11 '21 at 21:47
  • @LevM. I should have been more specific. I was testing out your solution by first writing to the file using ObjectOutputStream on FileOutputStream and testing it out having the program read the same objects from the file using ObjectInputStream on FileInputStream. I wrote 2 objects so I tried to call 2 objects by using the readObject() twice and then I got the StreamCorruptedException. – ultracookies Sep 14 '21 at 03:17
  • @Rigel then the issue may be what tgdavies linked to in the comment above. If you read that thread, it seems `ObjectOutputStrem` creates a header in the file that is suppose to exist only once. If you use the append method, trying to read the whole file in one go with a single `ObjectInputStream` will not work. – Lev M. Sep 14 '21 at 14:40
  • @LevM. Ahh I think I understand now. If I append everything into the file using the `ObjectOutputStream` and `FileOutputStream` objects and try to read the objects from the file using `ObjectInputStream`, it will just read the entire file as a whole rather than returning each individual object that was appended. Am I correct? – ultracookies Sep 14 '21 at 15:25
  • @Rigel basically yes, it will try to read it as if it was created by a single `ObjectOutputStream` and will fail because it was created by several different ones. – Lev M. Sep 14 '21 at 17:19