-1

I'm making a library that holds media items, such as dvds or video games, and saves a file for the list of the media items. I have to make a method that can read in the data from the save file, recreate the media items, and put them into the list. I was thinking of tokenizing it but I can't figure out how to get it started. Any suggestions? I'm using java. Each media item holds a String title, String format, Boolean borrowed, String name, and String date. This method is going to be used to load the save file upon starting the program.

  • for reading and writing data a simple text file would be more than enough if it is needed to be read by the user though if you are the one who needs to read the file I suggest looking into xml http://www.w3schools.com/xml/ though that link only shows you what xml is I suggest you do some of the research on implementing it into java. – SemperAmbroscus Dec 13 '14 at 00:02
  • Create a bean for the `MediaItem`, then serialize it using `XMLEncoder` as shown in [How to serialize Java 2D Shape objects as XML?](http://stackoverflow.com/q/26579729/418556) – Andrew Thompson Dec 13 '14 at 00:02
  • Why "String date" not "Date date"? – Andrew Thompson Dec 13 '14 at 00:03
  • Many choices for saving data from Java: Databases, Java Serialization, writing XML with [Simple library](http://simple.sourceforge.net) or JAXB, equivalents for JSON are popular nowadays, or use the new [Apache Commons CSV](http://commons.apache.org/proper/commons-csv/) library to read/write good old-fashioned [CSV (Comma-Separated Values)](https://www.rfc-editor.org/rfc/rfc4180.txt) or Tab-delimited files. Many options with many pros and cons, so too broad for a question here on StackOverflow. StackOverflow is aimed at specific programming problem-and-answer rather than long discussions. – Basil Bourque Dec 13 '14 at 03:14
  • Duplicate (closed as not constructive): http://stackoverflow.com/questions/13647657/what-are-good-methods-for-save-java-application-data – Basil Bourque Dec 13 '14 at 03:16

1 Answers1

0

You can use Serialization to save the data to a file and read it back. You won't be able to read the text though.

public class MediaSave {
    public static class Media implements Serializable{
        /**
         * 
         */
        private static final long serialVersionUID = 7452860232014574705L;
        public String title;
        public String format;
        public Boolean borrowed;
        public String name;
        public Date date;
        protected Media(String title, String format, Boolean borrowed,
                String name, Date date) {
            super();
            this.title = title;
            this.format = format;
            this.borrowed = borrowed;
            this.name = name;
            this.date = date;
        }
        public String toString(){
            return title + " " + format + " " + borrowed + " " + name + " " + " " +  date ;
        }
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException{
        Media[] media = new Media[4];
        media[0] = new Media("Avatar1", "dvd",true, "jon1", new Date());
        media[1] = new Media("Avatar2", "dvd",true, "jon2", new Date());
        media[2] = new Media("Avatar3", "dvd",true, "jon3", new Date());
        media[3] = new Media("Avatar4", "dvd",true, "jon4", new Date());
        Path p = Paths.get("resources/media.txt");
        File f = p.toFile();
        if(!f.exists()){
            f.createNewFile();
        } 


        try(ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream(f))){
            writer.writeInt(media.length);
            for(Media m : media){
            writer.writeObject(m);
            }
        }
        try(ObjectInputStream reader = new ObjectInputStream(new FileInputStream(f))){  
            int cnt = reader.readInt();
            for(int i =0 ; i != cnt;i++){
                     Media m=(Media)reader.readObject();
                     System.out.println(m.toString());
            }
        }
    }
}
dan b
  • 1,172
  • 8
  • 20