In: Computer Science
5) File I/O
A) Compare and contrast InputStream/OutputStream based file I/O to Scanner/Printwriter based file I/O:
B) Discuss Serialization, what is it? What are the processes and features? What is the function of the keyword Transient?
A. Compare and contrast InputStream/OutputStream based file I/O to Scanner/Printwriter based file I/O
The objective of InputStream and OutputStream is to extract various approaches to information and yield: regardless of whether the stream is a record, a website page, or the screen shouldn't make any difference. The only thing that is in any way important is that you get data from the stream (or send data into that stream). Stream here is referred to anything having more than one bit of character.
InputStream is utilized for some things that you read from.
OutputStream is utilized for some things that you write to.
While on the other hand, Scanner/Printwriter is normally used when we know input/output is of type string or of primitive types.
A Scanner object developed with System.in filters the standard information stream for gatherings of characters. A portion of its techniques convert a gathering characters to a particular information type. For instance, the nextInt() strategy peruses in a gathering of characters that can be changed over to an int.
PrintWriter is used to send characters to a text file.
B. Discuss Serialization, what is it? What are the processes and features? What is the function of the keyword Transient?
Serialization is a system of changing over the condition of an item into a byte stream. Deserialization is the turn around process where the byte stream is utilized to reproduce the real Java object in memory.
The byte stream created is platform independent. So, the object serialized on one platform can be deserialized on a different platform.
To make a Java object serializable we implement the java.io.Serializable interface.
The ObjectOutputStream class contains
writeObject() method for serializing an
Object.
public final void writeObject(Object obj) throws IOException
The ObjectInputStream class contains readObject() method for deserializing an object.
public final Object readObject() throws IOException, ClassNotFoundException
Advantages of Serialization
1. To save/persist state of an object.
2. To travel an object across a network.
Transient is a Java catchphrase which denotes a a member variable not to be serialized when it is persevered to stream of bytes. At the point when an item is moved through the system, the article should be 'serialized'. Serialization changes over the article state to sequential bytes. Those bytes are sent over the system and the item is reproduced from those bytes. Member variable set apart by the java transient watchword are not moved; they are lost deliberately.