For a class I'm working on, I have to create a program that writes a user-inputted list of songs to a binary file and then reads it to the console. This is done with two separate programs, one that processes data and one that gets user input. Whenever I try to write more than one song to the file, it replaces the last one instead of being listed along with it. How do I fix this?
Here's the relevant code for the data-processing program:
public void writeSong( String fileName, Song s )
{
DataOutputStream out = null;
try
{
out = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream( fileName )));
out.writeUTF( s.title );
out.writeUTF( s.artist );
out.writeUTF( s.genre );
System.out.println( "Song saved" );
out.close();
}
catch( Exception e )
{
System.out.println( "Song not saved" );
}
}
Here's the relevant code for the user-input program:
Scanner t = new Scanner(System.in);
System.out.println( "Song title: " );
String m = t.nextLine();
Scanner a = new Scanner(System.in);
System.out.println( "Song artist: " );
String n = a.nextLine();
Scanner g = new Scanner(System.in);
System.out.println( "Song genre: " );
String o = g.nextLine();
Song s = new Song();
s.setTitle( m );
s.setArtist( n );
s.setGenre( o );
process.writeSong( fileName, s );