Sie sind auf Seite 1von 10

Serialization is a mechanism of converting the state of an object into a byte

stream. It is the mechanism that provides the ability to an object to write a


copy of it and all the other objects it references in an external file through
the ObjecctOutputStream class. It can write data structures, graphs, Jframes,
or any object irrespective of its type. It is the conversion of a Java object
into a static stream (sequence) of bytes which can then be saved to a
database or transferred over a network

The reverse process is called deserializatio

Thus use of object serialization is as:

 A system for persisting object, i.e.; writing their properties on external


files or disk, or saving them to a database.
 A system for remote procedure calls.
 A system for distributing objects, for example in software components
like COM, COBRA.
 A system for identifying changes in time varying data.

it's mainly use in networks where we want to travel an object over network.


The serialized stream can be encrypted, authenticated and compressed,
supporting the needs of secure Java computing

Working:

 For serializing the object, we call


the writeObject() method ObjectOutputStream, and for deserialization
we call the readObject() method of ObjectInputStream class.
 We must have to implement the Serializable interface for serializing
the object.
 Serializable is a marker interface (has no data member and method).
It is used to "mark" Java classes so that the objects of these classes
may get a certain capability. The Cloneable and Remote are also
marker interfaces.
 If we want to serialize one object but do not want to serialize specific
fields, then we can mark those fields as transient.
 All the static fields belong to the class instead of the object, and the
serialization process serializes the object so static fields can not be
serialized

 Serialization does not care about access modifiers of the field such
as private. All non transient and non static fields are considered part
of an object's persistent state and are eligible for serialisation.
 We can assign values to final fields in conscrutors only and
serialization process do not invoke any constructor but still it can
assign values to final fields.
Introduction:
A stream can be defined as a sequence of data.

Java provides I/O Streams to read and write data where, a Stream represents an
input source or an output destination which could be a file, i/o devise, other program
etc.
In general, a Stream will be an input stream or, an output stream.
Based on the data they handle there are two types of streams −

 Byte Streams 
 Character

The Byte Stream Classes


 InputStream: Top level abstract class for byte-oriented input stream.
o ByteArrayInputStream: An instance of this class contains an internal
buffer to read bytes stream.
o FilterInputStream: An instance of this class contains some other input
stream as a basic source of data for further manipulation.
 BufferedInputStream: This enables
a FilterInputStream instance to make use of a buffer for input data.
 DataInputStream: An instance of this class enables reading
primitive Java types from an underlying input stream in a machine-
independent manner.
 LineNumberInputStream: An instance of this class aids in
keeping track of the current line number of the input stream.
 PushbackInputStream: This provides the ability to push back,
or "unread," a data byte after reading it.
 FileInputStream: An instance of this class is used to obtain input
bytes from a file in a file system.
 ObjectInputStream: An instance of this class is used to
deserialize an object after it has been serialized by ObjectOutputSteam.
 PipedInputStream: An instance of this class provides a pipe or
buffer for an input byte that works in the FIFO manner.
 SequenceInputStream: An instance of this class represents a
logical concatenation of two or more input streams which are read in
sequence, one after another.
 OutputStream: Top-level abstract class for byte-oriented input stream.
 ByteArrayOutputStream: An instance of this class contains an
internal buffer to write a bytes stream.
 FilterOutputStream: An instance of this class contains some
other output stream as a basic source of data for further manipulation.
 BufferedOutputStream: This enables
a FilterOutputStream instance to make use of a buffer for output data.
 DataOutputStream: An instance of this class enables writing
primitive Java types to an underlying output stream in a machine-
independent manner.
 PrintStream: This empowers the OutputStream objects with
the ability to print representations of various data values
conveniently.
 FileOutputStream: An instance of this class is used to output a
stream for writing data to a file or to a file descriptor.
 ObjectOutputStream: An instance of this class is used to serialize
an object which can be deserialized with ObjectInputStream.
 PipedOutputStream: An instance of this class provides a pipe or
buffer for output byte that works in the FIFO manner.
.

The Character Stream Classes


At the top of the character stream class hierarchy, there are two abstract classes: Reader for
character-oriented input and Writer for character-oriented output operations. The
hierarchical layout is as follows:

 Reader: Top-level abstract class to read to character streams.


o BufferedReader: Provides an in-between buffer for efficiency while
reading text from character input stream.
 LineNumberReader: Uses a buffered character input stream
that keeps track of line numbers.
 CharArrayReader: Implements an auto-increasing character
buffer that may be used as a reader.
 FilterReader: An instance of this class is used for reading
character files.
 PushbackReader: This enables a character to be pushed back
into the stream after reading.
 InputStreamReader: An instance of this class provides a bridge
from byte streams to character streams. Bytes are decoded into characters
using a specified character set.
 FileReader: An instance of this class is used for reading
character files.
 PipedReader: Uses a pipe for character input stream.
 StringReader: Character output stream reader from source
string.
 Writer: Top-level abstract class to write to character streams.
 BufferedWriter: Provides an in-between buffer for efficiency
while writing text to a character output stream.
 CharArrayWriter: Implements an auto-increasing character
buffer that may be used as a writer.
 FilterWriter: Abstract class for writing filtered character streams.
 OutputStreamWriter: An instance of this class provides a bridge
between character streams and byte streams. Characters are encoded into
bytes using a specified character set.
 FileWriter: An instance of this class is used for writing
character files.
 PipedWriter: Uses a pipe for character output stream.
 PrintWriter: Prints a formatted representation of an object to a
test-output stream.
 StringWriter: Character output stream is collected in a string
buffer and may be used for constructing a string.

Standard Streams
In Java, 3 streams are created for us automatically. All these streams are attached with
the console.

1) System.out: standard output stream

2) System.in: standard input stream

3) System.err: standard error stream

Conclusion
A Java stream acts as a file handling wrapper that operates according to the corresponding
I/O constructs. In many cases, character-oriented stream classes and byte-oriented stream
classes function in a very similar fashion. But, that does not mean that they
are not different. The stream classes defined in the java.io packages are rather simple and
unsophisticated, but they do serve their purpose.

import java.io.*;
import java.util.*;

class WriteText
{
public static void main(String[] args)
{
try {
String text = "Your sample content to save in a text
file.";
BufferedWriter out = new BufferedWriter(new
FileWriter("sample.txt"));
out.write(text);
out.close();
}
catch (IOException e)
{
System.out.println("Exception ");
}

return ;
No. String StringBuffer

1 String class is StringBuffer class is mutable.


) immutable.

2 String is slow and StringBuffer is fast and consumes less memory when you cancat st
) consumes more
memory when you
concat too many
strings because
every time it
creates new
instance.

3 String class StringBuffer class doesn't override the equals() method of Object c
) overrides the
equals() method of
Object class. So
you can compare
the contents of two
strings by equals()
method.

}
}
Difference between String and
StringBuffer
There are many differences between String and StringBuffer. A list of differences
between String and StringBuffer are given below:

Basis String class String Buffer class


Length of string is fixed
Main Length of string is not fixed in string buffer
in the string class
There can be no
Modification modification in the There can be a modification in string buffe
string class
String in string class is
Mutable String in string buffer class is immutable
mutable
String class consumes
Memory String buffer class consumes less memory
more memory

omparison Chart
What is a String Class?

Java URL
The Java URL class represents an URL. URL is an acronym for Uniform Resource
Locator. It points to a resource on the World Wide Web. For example:

1. https://www.javatpoint.com/java-tutorial  

A URL contains many information:


1. Protocol: In this case, http is the protocol.
2. Server name or IP Address: In this case, www.javatpoint.com is the server
name.
3. Port Number: It is an optional attribute. If we write
http//ww.javatpoint.com:80/sonoojaiswal/ , 80 is the port number. If port
number is not mentioned in the URL, it returns -1.
4. File Name or directory name: In this case, index.jsp is the file name.

5. Commonly used methods of Java


URL class
6. The java.net.URL class provides many methods. The important methods of URL
class are given below.

Method Description

public String it returns the protocol of the URL.


getProtocol()

public String getHost() it returns the host name of the URL.

public String getPort() it returns the Port Number of the URL.

public String getFile() it returns the file name of the URL.

public String it returns the authority of the URL.


getAuthority()

public String toString() it returns the string representation of the URL.

public String getQuery() it returns the query string of the URL.

public String it returns the default port of the URL.


getDefaultPort()

public URLConnection it returns the instance of URLConnection i.e. associated with this URL.
openConnection()

public boolean it compares the URL with the given object.


equals(Object obj)

public Object it returns the content of the URL.


getContent()
public String getRef() it returns the anchor or reference of the URL.

public URI toURI() it returns a URI of the URL.

List the advantages of JDBC


1. Provide Existing Enterprise Data
Businesses can continue to use their installed databases and
access information even if it is stored on different database
management systems.

2. Simplified Enterprise Development


The combination of the Java API and the JDBC API makes
application development easy and cost effective.

3. Zero Configuration for Network Computers


No configuration is required on the client side centralizes software
maintenance. Driver is written in the Java, so all the information
needed to make a connection is completely defined by the JDBC
URL or by a DataSource object. DataSource object is registered
with a Java Naming and Directory Interface (JNDI) naming service.

4. Full Access to Metadata


The underlying facilities and capabilities of a specific database
connection need to be understood. The JDBC API provides metadata
access that enables the development of sophisticated applications.

5. No Installation
A pure JDBC technology-based driver does not require special
installation.

6. Database Connection Identified by URL


The JDBC API includes a way to identify and connect to a data
source, using a DataSource object. This makes code even more
portable and easier to maintain.

 GET Method
 The GET method sends the encoded user information appended to the page
request. The page and the encoded information are separated by
the ? (question mark) symbol as follows −

 http://www.test.com/hello?key1 = value1&key2 = value2


 The GET method is the default method to pass information from browser to
web server and it produces a long string that appears in your browser's
Location:box. Never use the GET method if you have password or other
sensitive information to pass to the server. The GET method has size
limitation: only 1024 characters can be used in a request string.
.

 POST Method
 A generally more reliable method of passing information to a backend
program is the POST method. This packages the information in exactly the
same way as GET method, but instead of sending it as a text string after a ?
(question mark) in the URL it sends it as a separate message. This message
comes to the backend program in the form of the standard input which you
can parse and use for your processing. Servlet handles this type of requests
using doPost() method.

 A socket is one endpoint of a two-way communication link between two


programs running on the network. A socket is bound to a port number
so that the TCP layer can identify the application that data is destined to
be sent to.

TCP/IP sockets are used to implement reliable, bidirectional, persistent, point-


to-point, and stream -based connections between hosts on the Internet.
A socket can be used to connect Java's I/O system to other programs that may
reside either on the local machine or on any other machine on the Internet.

Das könnte Ihnen auch gefallen