Java Module 4
Java Module 4
Input and output (I/O) are the most commonly used operations in any program.
Reading data from the keyboard and writing data on to the monitor form the basic input and
output operations. It is essential that a programmer knows how to read the data from files and
write data to files. Java uses streams to handle data input and output.
● Input streams act as sources of data and output streams act as destinations of data.
● The java.io package defines various classes and methods for reading and writing files
and handling data streams.
● Programs can get inputs from a data source by reading a sequence of characters from
the input stream.
● Similarly, programs can produce outputs by writing a sequence of characters on to an
output stream .
System.in.read()
System.out println();
These two form the basic input and output streams, which are managed by the System
class; they use the standard input and standard output streams, respectively.
Java I/O
import java.io.* ;
● In general, streams are classified into two types known as character streams and the
bytestreams.
● The java.io package provides two sets of class hierarchies to handle character and byte
streams for reading and writing
1. InputStream and OutputStream classes are operated on bytes for reading and
writing,respectively.
2. Classes Reader and Writer are operated on characters for reading and writing, respectively.
There are two other classes that are useful for handling input and output. These are File class
and RandomAccessFile class.
A brief description of these four classes of java.io package are given in Table 7.1.
1.Character streams
● Reader and Writer are abstract super-classes for streaming 16-bit character inputs and
outputs, respectively.
● Methods of these classes throw the Exception exception under error conditions.
● All the methods in the Writer class have return type void.
(ii) those that also process the data that was read/written. Figures 7.1 and 7.2 show the class
hierarchies for the Reader and Writer classes. The descriptions of Reader and Writer classes
are summarized in Table 7.2.
2.Byte streams
● Byte streams are used in a program to read and write 8-bit bytes.
● InputStream and OutputStream are the abstract super-classes of all byte streams
● InputStream and OutputStream provide the Application Program Interface (API) and
partial implementation for input streams (streams that read bytes) and output streams
(streams that write bytes).
● These streams are typically used to read and write binary data such as those related to
images and sounds.
● Methods of these two classes throw the IOException.
● All methods of the OutputStream will have the return type void.
The hierarchies of InputStream class and OutputStream class are shown in Figures 7.3 and 7.4.
Filter Streams
● The java.io package provides a set of abstract classes that define and partially
implement Filter streams .
● A Filter stream filters data as it is being read from or written to the stream.
● The two filter streams for reading and writing data are FilterInputStream and
FilterOutputStream, respectively.
● A filter stream is constructed on another stream. That is, every filtered stream must be
attached to another stream.
● This can be achieved by passing an instance of InputStream or OutputStream to a
constructor.
● This implies that filter streams are chained. Multiple filters can be added by chaining
them to a stream of bytes.
● The read method in a readable filter stream reads input from the underlying stream,
filters it and passes on the filtered data to the caller.
● The write method in a writable filter stream filters the data and then writes it to the
underlying stream.
Most filter streams provided by the java.io package are sub-classes of FilterInputStream and
FilterOutputStream, and are listed below:
RandomAccessFile
● Thus, RandomAccessFile objects can be created either from a string containing the file
name or from a File object.
● The mode represents the type of access to the file, for example, 'r' for read and rw' for
read and write.
● There are two other modes, namely, 'rws' and 'rwd', 'rws' opens the file as read and
write and stores the updations made on the file data or metadata in the synchronized
way, whereas, 'rwd' deals with the file data but not the metadata.
● Here, metadata refers to the data about the files.
● Random AccessFile throws an IOException if an I/O error has occurred.
Serialization
● Serialization is the technique most commonly used for network applications and
persistent storage of objects, in order to maintain the reference of a remote object.
● Storing the state of the object into a file is often referred to as serialization. whereas,
reading the object state from a stored file is referred to as deserialization.
● The streams ObjectOutputStream and the ObjectInputStream are used for object
serialization and deserialization, respectively.
● These classes allow us to read/write objects from to streams converting from internal to
external 8-bit representation.
● These two classes throw the exceptions IOException and StreamCorruptedException.
● The two methods that are useful for object serialization are readObject() on
ObjectInputStream and write Object(Object obj) on the ObjectOutputStream.
● To make any object serializable, the object of the class must implement the serializable
interface of the java.io package.
● That is, we have to specify implements Serializable in the definition of the class, the
objects of which we would like to read/write from to files.
APPLETS
INTRODUCTION
● One of the main features of Java is the applet.
● Applets are dynamic and interactive programs.
● Applets are usually small in size and facilitate event-driven applications that can be
transported over the web.
● An applet is a Java code that must be executed within another program.
● It mostly executes in a Java-enabled web browser.
● An applet can be embedded in an HTML document using the tag <APPLET> and can be
invoked by any web browser.
● Parameters can be passed to an applet using the tag <PARAM> in the HTML document.
● Applets can also be executed using the appletviewer utility provided in the JDK kit.
● The Applet class is defined in the package java.applet. All the basic capabilities of
applets are provided in this class.
● To write Java applets, the class Applet present in the java.applet package must be
extended. The Applet class has to be imported as shown below:
import java.applet.Applet
Like any other Java program, the applet's class name must match with the name of
the file in which it is written. Applets do not contain the main() that is required to run a Java
application and thus they cannot run on its own, however, usually applets run in a container that
provides the missing code. The main features of applet include the following:
● It provides a GUI.
Note that it is possible to execute applets using the Java interpreter (using the main()
method) if the class is extended with Frame. Usually, applets interact with the AWT controls and
it is necessary to import the awt package (java.awt.*) and also the event package (java
awt.event.*) for event handling.
Java programs consist of applications and applets. Java applications are stand-alone
programs and typically executed using the Java interpreter. On the other hand, Java applets are
executed in a web browser or in a special window known as the Appletviewer; however, the
compilation stages of both are the same.
The structure of applets differs from that of application programs for example applets
do not have the main() method. Applets do not interact with the console operations (that is, I/O
operations) like application programs do. Applets allow the user to interact through the AWT.
Applets differ from applications in the way they are executed. An application is started when its
main() method is called. In the case of applets, an HTML file has to be created, which tells the
browser what to load and how to run the applet. Java, however, allows a programmer to create
software code that can function both as a Java application and as a Java applet using a single
class file. Table 8.1 illustrates the similarities and differences between Java applications and
applets.
Applet Life-cycle
An applet has a life-cycle, which describes how it starts, how it operates and how it ends.
Figure 8.2 shows the life-cycle of an applet.
The life-cycle consists of four methods: init(), start(),stop() and destroy(). These
methods are used as follows:
● init(): It is called only once when the applet is first loaded and created by the browser.
In addition, there is a method paint() which is used to draw the applet for display.
When an applet is loaded (or reloaded), the browser creates an instance of the applet into a
browser and then initializes it before starting the applet. When a user leaves (or minimizes) a
web page, the browser stops the applet and when the user returns to the page the browser
starts the applet again. The default implementations of the init(), start(), stop(), destroy) and
paint() methods do not have any code and, thus, do nothing at all.
● The init() method of the Applet class provides the capability to initialize the applet
variables.
● That is this method can be overridden to perform basic initialization of an applet
● The method init() is called when the applet is created and loaded for the first time into a
browser (or reloaded).
● This method is called only once in the applet's life-cycle.
● The start() method is called to start (or restart) the execution of the applet
● The browser can call this method after the init() method is completed.
● The start() method runs whenever the applet becomes visible .
● That is, this method is called each time the applet is going to be displayed on the screen.
● For example, while navigating through the web, whenever a user returns to a web page
in which the applet is written, the applet is restarted.
● Thus, unlike the init() method, an applet can start many times throughout its life-cycle.
● The start() method can also be called if the applet is stopped and to start animation and
play sounds.
● A single start() method can start up one or several threads.
● The APPLET tag of HTML is used to start an applet either from a web browser or an
applet viewer.
● The HTML tag allows a Java applet to be embedded in an HTML document.
● A number of optional attributes can be used to control an applet's appearance, for
example, the dimensions of the applet panel.
● The attributes code, width and height are mandatory and all other attributes are options,
which are shown in brackets ([]).
● The attributes need not follow the order that was specified in the above syntax.
code :
The name of the Java applet. Class file, which contains the compiled applet code.
width
This refers to the width of the applet panel specified in pixels in the browser window.
height
This refers to the height of the applet panel specified in pixels in the browser window.
The width and height attributes specify the applet's display area. This applet area does not
include any windows or dialogue boxes that the applet shows.
codebase
This refers to the base URL of the applet. That is, the URL of the directory that contains the
applet class file. If this attribute is not specified then the HTML document's URL directory is
taken as the CODEBASE.
archive
There are one or more archive files containing Java classes and other resources that will be
preloaded. The classes are loaded using an instance of the AppletClassLoader class with the
given codebase. The archives in archivelist are separated by a comma (,).
alt
This refers to text to be displayed if the browser understands the applet tag but cannot run
Java applets.
name
This refers to a name for the applet instance which makes it possible for applets to
communicate with each other on the same HTML page. The getApplet() method, which is
defined in the AppletContext interface, can be used to get the applet instance using name.
align
This refers to the alignment of the applet. The possible attribute values are LEFT, RIGHT,
TOP, BOTTOM, MIDDLE, BASELINE, TEXTTOP, ABSMIDDLE and ABSBOTTOM
vspace
This refers to the space, specified in number of pixels, used as a margin above and below the
applet.
hspace
This refers to the space, specified in number of pixels, used as a margin to the left and right of
the applet.
param
The param tag, specifies an applet parameter as a name-value pair. The name is the
parameters name, value is its value. The values of these parameters can be obtained using the
getParameter() method.
alternateHTML
This is ordinary HTML to be displayed in the absence of Java or if the browser does not
understand the applet tag. This is different from the alt attribute which understands the
applet tag but cannot run, whereas the alternate HTML tag is provided when a browser does not
support applets.