Java StreamCorrupted Exception, using JSON -
i'm not sure why getting error @ bottom of code? appreciated. understand has use of objectoutputstream or objectinputstream? not entirely positive. in advance.
//main
import java.io.serializable; class libraryofmoviedescriptions implements serializable { public static void main(string[] args){ movielibrary movielibrary; movielibrary = new movielibrary("movies.json"); movielibrary.tojsonfile("movies.json"); } }
//movie description class
import org.json.jsonexception; import org.json.jsonobject; import org.json.jsonarray; import java.io.serializable; public class moviedescription implements serializable { private string title; private string rating; private string release; private string runtime; private string plot; private string filename; private string genre; private string actors; public jsonobject tojsonobject() throws jsonexception { jsonobject obj = new jsonobject(); obj.put("title", title); obj.put("rated", rating); obj.put("released", release); obj.put("runtime", runtime); obj.put("plot", plot); obj.put("filename", filename); jsonarray = new jsonarray(); string[] sarray = this.actors.split(" , "); for(int = 0; < sarray.length; i++){ a.put(i); } obj.put("actors", a); jsonarray g = new jsonarray(); string[] garray = this.genre.split(" , "); for(int = 0; < garray.length; i++){ g.put(i); } obj.put("genre", g); return obj; } public moviedescription(jsonobject jsonobj) throws jsonexception{ this.title = jsonobj.getstring("title"); this.rating = jsonobj.getstring("rating"); this.release = jsonobj.getstring("released"); this.plot = jsonobj.getstring("plot"); this.runtime = jsonobj.getstring("runtime"); this.filename = jsonobj.getstring("filename"); jsonarray g = jsonobj.getjsonarray("genre"); for(int = 0; < g.length(); i++){ this.genre += g.get(i) + ", "; } jsonarray = jsonobj.getjsonarray("actors"); for(int = 0; < a.length(); i++){ this.actors += a.get(i) + ", "; } } public moviedescription(){ title = " "; rating = " "; release = " "; runtime = " "; plot = " "; filename = " "; genre = " "; actors = " "; } public moviedescription(string title, string rating, string release, string runtime, string plot, string filename, string genre, string actors){ this.title = title; this.rating = rating; this.release = release; this.runtime = runtime; this.plot = plot; this.filename = filename; this.genre = genre; this.actors = actors; } public void settitle(string title){ this.title = title; } public string gettitle(){ return title; } public void setrating(string rating){ this.rating = rating; } public string getrating(){ return rating; } public void setrelease(string release){ this.release = release; } public string getrelease(){ return this.release; } public void setruntime(string runtime){ this.runtime = runtime; } public string getruntime(){ return runtime; } public void setplot(string plot){ this.plot = plot; } public string getplot(){ return plot; } public void setfilename(string filename){ this.filename = filename; } public string getfilename(){ return filename; } public void setgenre(string genre){ this.genre = genre; } public string getgenre(){ return genre; } public void setactors(string actors){ this.actors = actors; } public string getactors(){ return actors; } public string tostring(){ string string = ("title: " + title + "\n" + "rating: " + rating + "\n" + "released: " + release + "\n" + "runtime: " + runtime + "\n" + "plot: " + plot + "\n" + "filename: " + filename + "\n" + "genre: " + genre + "\n" + "actors: " + actors + "\n"); return string; } }
//movie library class
import org.json.jsonobject; import java.io.ioexception; import java.io.file; import java.util.arraylist; import java.util.list; import java.io.fileinputstream; import java.io.fileoutputstream; import org.json.jsontokener; import java.io.serializable; import java.io.objectinputstream; import java.io.objectoutputstream; public class movielibrary implements serializable { private list<moviedescription> movielib = new arraylist<moviedescription>(); private int arraysize; public movielibrary(){ arraysize = 0; } public boolean isempty(){ return arraysize == 0; } public moviedescription get(string atitle){ int = indexof(atitle); if(i == -1){ return null; } return movielib.get(i); } public boolean add(moviedescription aclip){ movielib.add(aclip); arraysize++; return true; } public boolean remove(string atitle){ int = indexof(atitle); if(i != -1){ movielib.remove(i); arraysize--; } return true; } public string[] gettitles(){ string[] s = new string[movielib.size()]; for(int = 0; < movielib.size(); i++){ s[i] = movielib.get(i).gettitle(); } return s; } private int indexof(string atitle){ for(int = 0; < movielib.size(); i++) if(((movielib.get(i)).gettitle()).equals(atitle)){ return i; } return -1; } public movielibrary(string jsonfile){ objectinputstream in = null; file infile = null; jsonobject jsonobj = null; string[] titles; try{ infile = new file(jsonfile); in = new objectinputstream(new fileinputstream(infile)); jsonobj = new jsonobject(new jsontokener(in)); titles = jsonobject.getnames(jsonobj); system.out.println("adding movies..."); for(int = 0; < titles.length; i++){ jsonobject temp = jsonobj.getjsonobject(titles[i]); moviedescription movies = new moviedescription(temp); movielib.add(movies); } system.out.println(this.gettitles()); in.close(); } catch(exception e){ e.printstacktrace(); if(in != null){ try{ in.close(); } catch(ioexception z){ z.printstacktrace(); system.exit(0);; } } } } public void tojsonfile(string jsonfilename){ jsonobject jsonobj = new jsonobject(); objectoutputstream out = null; try{ for(int = 0; < movielib.size(); i++) jsonobj.put(movielib.get(i).gettitle(),movielib.get(i).tojsonobject()); out = new objectoutputstream(new fileoutputstream(jsonfilename)); out.writeobject(jsonobj.tostring()); out.close(); } catch(exception e){ e.printstacktrace(); if(out != null){ try{ out.close(); } catch(ioexception z){ z.printstacktrace(); system.exit(0); } } } } }
and getting error:
java.io.streamcorruptedexception: invalid stream header: 7b202022 @ java.io.objectinputstream.readstreamheader(unknown source) @ java.io.objectinputstream.<init>(unknown source) @ movielibrary.<init>(movielibrary.java:79) @ libraryofmoviedescriptions.main(libraryofmoviedescriptions.java:11)
Comments
Post a Comment