Object Oriented Programming Using Java
Subject Code: 033CSC011
Unit - IV
Sajeetkumar Pujar
Assistant Professor of Computer Science
Govt. First Grade College, Haliyal
I/O Programming:
• The variables and arrays are used to store the data. This approach
leads to the following problems:
– The data is lost either when a variable goes out of scope or
when the program is terminated. That is, the storage is
temporary.
– It is difficult to handle large volumes of data using variables
and arrays.
• To overcome these difficulties, the files are used.
• The files are stored on secondary storage devices such as floppy
disk or hard disk.
Concept of Streams:
• In file processing, input refers to the flow of data into a program
and output means the flow of data out of a program.
• Input to a program may come from the keyboard, the mouse, the
memory, the disk, etc.
• Similarly, output from a program may go to the screen, the
printer, the memory, the disk etc.
Concept of Streams:
• Java uses the concept of streams to represent the ordered
sequences of data.
• A stream is a path with which data flows from source to
destination.
• Both the source and the destination may be physical devices or
programs.
Concept of Streams:
• Java streams are classified into two basic types.
– Input stream
– Output stream
• An input stream extracts (reads) data from the source (file) and
sends it to the program.
• Similarly, an output stream takes data from the program and
sends (writes) it to the destination (file).
Stream classes:
• The [Link] package contains a large number of stream classes
that provide capabilities for processing all types of data.
• These classes are categorized into two groups:
– Byte Stream Class / Binary Stream Class
– Character Stream Class / Text Stream Class
Byte Stream classes:
• ByteStream classes are used to read bytes from the input stream
and write bytes to the output stream.
• In other words, ByteStream classes read/write the data of 8-bits.
• Binary/Byte Stream files are not human readable.
• Byte streams are especially used with binary files such as
executable files, image files, and files in low-level file formats
such as .zip, .class, .obj, and .exe.
• Java provides two kinds of byte stream classes:
– InputStream class
– OutputStream class
InputStream and OutputStream classes:
• InputStream and OutputStream classes are abstract classes.
These are the root classes for reading and writing binary I/O data.
• Since InputStream and OutputStream classes are abstract classes,
we cannot create objects of these classes. We must use
subclasses of these classes to create objects.
Character Stream classes:
• Character Stream classes can be used to read and write 16-bit
Unicode characters.
• Character streams are more efficient than byte streams. They are
mainly used for reading or writing to character or text-based I/O
such as text files, text documents, XML, and HTML files.
• Text files are human readable. For example, a .txt file that
contains human-readable text. This file is created with a text
editor such as Notepad in Windows.
• Java provides two kinds of Character/Text stream classes:
– Reader Stream class
– Writer Writer class
Reader Stream class:
• Reader stream classes are used for reading characters from files.
• Reader is an abstract superclass for all other subclasses such as
BufferedReader, StringReader, CharArrayReader, etc.
Writer Stream class:
• Writer stream classes are used to write characters to a file. In
other words, They are used to perform all output operations on
files.
• Writer stream classes are similar to output stream classes with
only one difference that output stream classes use bytes to write
while writer stream classes use characters to write.
Collections in Java:
• What is Collection?
– A Collection represents a single unit of objects, i.e., a group.
• What is a framework?
– It provides readymade architecture.
– It represents a set of classes and interfaces.
Collections in Java:
• The Collection in Java is a framework that provides an
architecture to store and manipulate the group of objects.
• Java Collection means a single unit of objects.
• Java Collections can achieve all the operations that you perform
on a data such as searching, sorting, insertion, manipulation, and
deletion.
• Java Collection framework provides many interfaces (Set, List,
Queue, Deque) and classes (ArrayList, Vector, LinkedList,
PriorityQueue, HashSet, LinkedHashSet, TreeSet).
Introduction to JavaBeans:
• JavaBeans is a portable, platform-independent model written in
Java Programming Language. Its components are referred to as
beans.
• In simple terms, JavaBeans are classes which encapsulate several
objects into a single object.
• JavaBeans contains several elements like Constructors,
Getter/Setter Methods and much more.
• It is a software component that has been designed to reuse in
variety of different environments.
Introduction to JavaBeans:
• JavaBeans has several conventions that should be followed:
– Beans should have a default constructor (no arguments).
– Beans should provide getter and setter methods.
– A getter method is used to read the value.
– To update the value, a setter method should be called.
– Beans should implement [Link]
Network Programming in Java:
• Java Networking is a concept of connecting two or more
computing devices together so that we can share resources.
• Java socket programming provides facility to share data between
different computing devices.
• Advantage of Java Networking:
– Sharing resources
– Centralize software management
Network Programming in Java:
• The widely used Java networking terminologies:
– IP Address
– Protocol
– Port Number
– MAC Address
– Connection-oriented and connection-less protocol
– Socket
Network Programming in Java:
• IP Address:
– IP address is a unique number assigned to a node of a
network e.g. [Link] . It is composed of octets that range
from 0 to 255.
• Protocol:
– A protocol is a set of rules basically that is followed for
communication. For example:
– TCP
– FTP
– Telnet
– SMTP
– POP
Network Programming in Java:
• Port Number
– The port number is used to uniquely identify different
applications. It acts as a communication endpoint between
applications.
– The port number is associated with the IP address for
communication between two applications.
• MAC Address
– A MAC (Media Access Control) address is a 12-digit
hexadecimal number assigned to each device connected to
the network.
– For example, an ethernet card may have a MAC address of
00:0d:83::b1:c0:8e.
Network Programming in Java:
• Connection-oriented and connection-less protocol
– In connection-oriented protocol, acknowledgement is sent by
the receiver. So it is reliable but slow. The example of
connection-oriented protocol is TCP.
– But, in connection-less protocol, acknowledgement is not sent
by the receiver. So it is not reliable but fast. The example of
connection-less protocol is UDP.
• Socket
– A socket is an endpoint between two way communications.
References:
• Programming with Java, E. Balagurusamy, 4th Edition, McGraw Hill Publications.
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]