javainputstreamoutputstreameofexception

Nothing being written to a .dat file


I am trying to create a program that reads a .dat file containing a HashMap object. If the .dat file is empty, it should write an empty HashMap object to the file, then, it should give the user options to edit the HashMap object.

When I tried this for the first time with an empty .dat file "playlist.dat", it attempted to write an empty HashMap object to the file, but then triggered an EOFException. I looked in the file and saw that it was empty.

The code handling this is here:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Scanner;

@SuppressWarnings("unchecked") //was the only way to stop the error: "Type safety: Unchecked cast from Object to HashMap<String,Song>" from happening

public class Main {
    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
        // Map of songs
        HashMap<String, Song> playList = new HashMap<String, Song>();

        // getting file name where it is stored
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the file name you stored/will store your playlist in: ");
        String fileName = input.nextLine();
        input.close();

        File file = new File(fileName);
        
        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file))); //using output streams to find the file requested by user

        if (file.length() == 0) {
            out.writeObject(playList);
            //System.out.println("Wrote playlist"); //debug
        } //sees if there is nothing in the file, and if there is nothing, writes a blank HashMap to it

        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file))); //This is where the EOFException is taking place. In the if statement above, it tests if the file is empty. Since playlist.dat is empty, it will attempt to write an empty HashMap playlist to the file, but since there is an EOFException and playlist.dat is empty, it is clearly not doing this.
        
        playList = (HashMap<String, Song>) in.readObject();
        System.out.println("Opening playlist at " + fileName + "."); //debug
        in.close();

/* other code continues this way... */

Solution

  • Basically you want to serialize an object, specifically an instance of HashMap and then de-serialize it.

    Serialization in java only stores the values of the class members. It uses a combination of the actual .class file together with the .dat file to de-serialize an object. Hence if you serialize an empty HashMap, the .dat file will be [almost] empty. In your case it is empty because you didn't close the file after writing it.

    Below code is a minimal example of serializing and de-serializing a HashMap. Note that it uses try-with-resources to ensure that the .dat files are closed after use. It also uses multi-catch to handle exceptions.

    Explanations of the code appear after it.

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.HashMap;
    
    public class SongList {
    
        public static void main(String[] args) {
            File f = new File("songlist.dat");
            try (FileOutputStream fos = new FileOutputStream(f);
                 ObjectOutputStream oos = new ObjectOutputStream(fos)) {
                HashMap<String, String> playList = new HashMap<>();
                oos.writeObject(playList);
            }
            catch (IOException xIo) {
                xIo.printStackTrace();
            }
            try (FileInputStream fis = new FileInputStream(f);
                 ObjectInputStream ois = new ObjectInputStream(fis)) {
                HashMap<?, ?> playList = (HashMap<?, ?>) ois.readObject();
            }
            catch (IOException | ClassNotFoundException x) {
                x.printStackTrace();
            }
        }
    }
    

    By the way, you should not close a Scanner that wraps standard input.