0

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 );
Becca
  • 35
  • 4
  • 2
    Use this constructor instead https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html#FileOutputStream(java.io.File,%20boolean) - the second parameter can tell it to append – Scary Wombat Aug 09 '17 at 01:42
  • Also maybe use try-with-resources so that the output stream can be automatically closed – Scary Wombat Aug 09 '17 at 01:43
  • Is there a reason you're using `DataOutputStream`? It's specifically for [Java Serialization](https://docs.oracle.com/javase/tutorial/jndi/objects/serial.html), which it doesn't look like you're doing. `OutputStream` lets you write arbitrary bytes (i.e. binary data) to a file. – dimo414 Aug 09 '17 at 01:44
  • it is similar https://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java – Serge Aug 09 '17 at 01:46
  • @dimo414 It's just what the class I'm taking used... no idea why – Becca Aug 09 '17 at 01:48
  • In that case I'd check with your instructor; they probably have a reason and can help you figure out the way they intended for you to use it. – dimo414 Aug 09 '17 at 01:52
  • Can you pass the `OutputStream` as an argument to `writeSong`? Also, you do not need so many `Scanner` instances opened on `System.in` (one suffices). – Elliott Frisch Aug 09 '17 at 01:57
  • @dimo414: `Object{Output,Input}Stream` is specific to serialization, `Data~` is more general (though not totallly) and a not-crazy choice here. – dave_thompson_085 Aug 09 '17 at 04:13
  • @dave_thompson_085 thanks for pointing that out, you're absolutely right. – dimo414 Aug 09 '17 at 05:12

0 Answers0