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... */