1 / 11

A Document

A Document. Dear John, I think we should just be friends…. 68 101 97 114 20 74 111 104 110 44. D e a r J o h n ,. A serial data “STREAM”. 68 101 97 114 20 74 111 104 110 44. 68 101 97 114 20 74 111 104 110 44. Storage on disk.

tanith
Download Presentation

A Document

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Document Dear John, I think we should just be friends… 68 101 97 114 20 74 111 104 110 44 D e a r J o h n ,

  2. A serial data “STREAM” 68 101 97 114 20 74 111 104 110 44 68 101 97 114 20 74 111 104 110 44

  3. Storage on disk SOF 68 101 97 114 74 111 104 110 44 EOF “streamed” to disk (an output) SOF 68 101 97 114 20 74 111 104 110 44 EOF Dear John, streamed from disk (an input)

  4. class infoClass { public String Name; public int Age; public infoClass( String n, int a ) { Name = n; Age = a; } } infoClass infoObject = new infoClass ( “Sherlock Holmes”, 39 ); Objects contain methods and info Contains info, so size is known and finite Contains no info, just placeholders

  5. Serializable - means “in a row” Sherlock Holmes 39 OBJECT turn into a data stream 83 104 101 114108 111 99 107…

  6. Serializable interface class infoClass implements Serializable { String Name; int Age; } • means that objects of this class can be turned into an orderly dependable data stream

  7. the Serializable Interface • no methods are involved, so you don’t have to write anything • called a “tagging” interface, because Java just adds variables to your object, to keep the place of your variables in a data stream • Objects that are serializable can be used by “streaming classes”: file I/O, network I/O

  8. what the computer does… class infoClass implements Serializable { String Name; int placeOfName = 1; int sizeOfName = 10; // bytes int Age; int placeofAge = 11; int sizeOfAge = 4; // bytes }

  9. Streaming to a byte array

  10. from the byte array ByteArrayInputStream bais = new ByteArrayInputStream( byteArray ) ObjectInputStream ois = new ObjectInputStream( bais ); AnyClass newObject = (AnyClass)ois.readObject(); ois.close();

  11. to and from streams • to a file: FileOutputStream target = new FileOutputStream("filename.ser"); ObjectOutputStream out = new ObjectOutputStream( target ); out.writeObject( anyObject ); out.close(); • from a file: FileInputStream source = new FileInputStream( "filename.ser" ); ObjectInputStream in = new ObjectInputStream( source ); AnyClass newObject = (AnyClass) in.readObject(); in.close();

More Related