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... */
Basically you want to serialize an object, specifically an instance of
HashMapand then de-serialize it.Serialization in java only stores the values of the class members. It uses a combination of the actual
.classfile together with the.datfile to de-serialize an object. Hence if you serialize an emptyHashMap, the.datfile 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.datfiles are closed after use. It also uses multi-catch to handle exceptions.Explanations of the code appear after it.
BufferedOutputStreamandBufferedInputStreamare not required.HashMap, java has no way of knowing what the types are for the entries in theHashMap. That's why you are getting the warning. Note that it is a warning and not an error. That's why I use the?wildcard when reading the.datfile.Song, I changed theHashMapvalue type toStringso as to keep things simple. You can continue to useSongin your code.FileOutputStreamcreates a new, empty file. Creating a newFileOutputStreamfor a file that already exists will remove the file contents. If you want to append to an existing file, use this constructor of classFileOutputStream.By the way, you should not close a
Scannerthat wraps standard input.