找不到生成的文件:FileOutPutStream

时间:2015-07-13 15:51:56

标签: java persistence outputstream fileoutputstream

我正在研究java项目(Media Player)的持久性。如果用户重新打开程序,我应该保存添加的文件并加载它们。

我正在使用图书馆,我存储用户添加的曲目/音乐会/专辑:

import java.io.Serializable;
import java.util.List;

public class MusicLibrary implements Displayable, Serializable {
private static MusicLibrary reference;
public static GenericList<SingleTrack> allTracks;
public static GenericList<Album> allAlbums;
public static GenericList<Concert> allConcerts;
public static GenericList<Artist> allArtists;

/**private constructor to keep users from instantiating the class more than once*/
private MusicLibrary(){
    allTracks = new GenericList<SingleTrack>();
    allAlbums = new GenericList<Album>();
    allConcerts = new GenericList<Concert>();
    allArtists = new GenericList<Artist>();

}

/**method that makes sure the class is instantiated only once*/
public static MusicLibrary getLibrary(){
    if(reference == null)
        reference = new MusicLibrary();

    return reference;

}
public void addTrack(SingleTrack musicTrack){
    allTracks.add(musicTrack);
}
public void addAlbum(Album album){
    allAlbums.add(album);
}
public void addConcert(Concert concert){
    allConcerts.add(concert);
}
public void addArtist(Artist artist){
    allArtists.add(artist);
}

public SingleTrack[] getTracks(){

    return (SingleTrack[]) allTracks.toArray();

}

/*public String displayTracks(){
    return allTracks.displayList();
}*/


public String getTitle() {
    return null;
}


public String forDisplay() {    
    return null;
}

public String getName(){
    return null;
}

public static class getLibrary {

    public getLibrary() {
    }
}
}

然后我有另一个名为PersistentLibrary的类,我在其中管理我的库的持久性(即:保存库并加载它):

package dj2.persistence;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import dj2.core.*;

public class PersistentMusicLibrary {

    private static String libraryStore = "library.ser";
    private static MusicLibrary library;
    private static ObjectInputStream libraryIn;
    private static ObjectOutputStream libraryOut;

    private PersistentMusicLibrary(){
        //don't let anyone instantiate me
    }

    public static MusicLibrary getInstance(){
        if(library == null){
            try{
                System.out.print("1");
                libraryIn = new ObjectInputStream(new FileInputStream(libraryStore));
                library = (MusicLibrary)libraryIn.readObject();
                libraryIn.close();
            }catch(Exception ex){
                library = MusicLibrary.getLibrary();
            }
        }
        return library;
    }


    public static void saveLibrary()throws IOException {
        if(library == null)
            return;
            //System.out.println("You are not null");
        libraryOut = new ObjectOutputStream(new               FileOutputStream(libraryStore));
        libraryOut.writeObject(library);
        libraryOut.close();
    }

}

问题是我的保存方法似乎不起作用,因为我无法在计算机上找到生成的文件,也不会抛出异常。 我在menuItem的actionListener中调用它&#34; save&#34; :

menuFileSave.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent ae){
     try{
        PersistentMusicLibrary.saveLibrary();
      }catch(IOException ioe){
          System.out.println("Fail to save");
      }
    }});

有人可以帮我调试吗?

0 个答案:

没有答案