You are on page 1of 29

SUBJECT NAME: Programming in Java

SUBJECT CODE: SITA1301


UNIT 3: LANG AND IO PACKAGES
Java. lang package - Wrapper Classes– Simple type wrappers –
Introduction – Input Stream and Output Stream classes - Data
Output Stream and Data Input Stream classes –FileInput Stream
– File Output Stream. - Reader and Writer Classes – File Reader
and File Writer
Wrapper class in Java
Wrapper class in java provides the mechanism to convert primitive into
object and object into primitive.
The eight classes of java.lang package are known as wrapper
classes in java. The list of eight wrapper classes are given below:
Primitive Type Wrapper Class
boolean Boolean
char Character Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
Wrapper class in Java
Wrapper class in java provides the mechanism to convert primitive into
object and object into primitive.

Sometimes you must use wrapper classes, for example


when working with Collection objects, such as ArrayList,
where primitive types cannot be used (the list can only
store objects):
ArrayList<int> myNumbers = new ArrayList<int>(); //
Invalid
ArrayList<Integer> myNumbers = new
ArrayList<Integer>(); // Valid
Wrapper class in Java
A Wrapper class is a class which contains the primitive data types (int, char, short,
byte, etc). In other words, wrapper classes provide a way to use primitive data
types (int, char, short, byte, etc) as objects. These wrapper classes come
under java.util package.
Why we need Wrapper Class
Wrapper Class will convert primitive data types into objects. The objects are
necessary if we wish to modify the arguments passed into the method (because
primitive types are passed by value).
The classes in java.util package handles only objects and hence wrapper
classes help in this case also.
Data structures in the Collection framework such as ArrayList and Vector store
only the objects (reference types) and not the primitive types.
The object is needed to support synchronization in multithreading.
Creating Wrapper Objects
To create a wrapper object, use the wrapper class instead of the
primitive type. To get the value, you can just print the object:
public class MyClass {
public static void main(String[] args)
{ Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt);
System.out.println(myDouble);
System.out.println(myChar); }}
Since you're now working with objects, you can use certain
methods to get information about the specific object.
For example, the following methods are used to get the
value associated with the corresponding wrapper object:
 intValue(), byteValue(), shortValue(), longValue(), floatValue(), doubleV
alue(), charValue(), booleanValue().
public class MyClass {
public static void main(String[] args)
{ Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt.intValue());
System.out.println(myDouble.doubleValue());
System.out.println(myChar.charValue()); }}
Another useful method is the toString() method, which is
used to convert wrapper objects to strings.
In the following example, we convert an Integer to a String,
and use the length() method of the String class to output the
length of the "string":
public class MyClass
{public static void main(String[] args)
{
Integer myInt = 100;
String myString = myInt.toString();
System.out.println(myString.length()); }}
Autoboxing in Wrapper Class
Autoboxing is used to convert primitive data types into corresponding objects
The automatic conversion of primitive data type into its corresponding wrapper class is known as
autoboxing, for example, byte to Byte, char to Character, int to Integer, long to Long, float to Float
boolean to Boolean, double to Double, and short to Short.
we do not need to use the valueOf() method of wrapper classes to convert the primitive into
objects.
Wrapper class Example: Primitive to Wrapper
public class WrapperExample1
{ public static void main(String args[])
{int a=20; //Converting int into Integer
Integer i=Integer.valueOf(a); //converting int into Integer
Integer j=a; //autoboxing, now compiler will write Integer.valueOf(a) internally
System.out.println(a+" "+i+" "+j);
}}
Output: 20 20 20
Unboxing is used to convert the Wrapper class object into corresponding primitive data types.
The automatic conversion of wrapper type into its corresponding primitive type is known as
unboxing. It is the reverse process of autoboxing.
we do not need to use the intValue() method of wrapper classes to convert the wrapper type into
primitives.
Wrapper class Example: Wrapper to Primitive
public class WrapperExample2
{
public static void main(String args[])
{
Integer a=new Integer(3); //Converting Integer to int
int i=a.intValue(); //converting Integer to int
int j=a; //unboxing, now compiler will write a.intValue() internally
System.out.println(a+" "+i+" "+j);
}}
Output:
333
Unboxing is used to convert the Wrapper class object into corresponding primitive data types.
The automatic conversion of wrapper type into its corresponding primitive type is known as
unboxing. It is the reverse process of autoboxing.
we do not need to use the intValue() method of wrapper classes to convert the wrapper type into
primitives.
Wrapper class Example: Wrapper to Primitive
public class WrapperExample2
{
public static void main(String args[])
{
Integer a=new Integer(3); //Converting Integer to int
int i=a.intValue(); //converting Integer to int
int j=a; //unboxing, now compiler will write a.intValue() internally
System.out.println(a+" "+i+" "+j);
}}
Output:
333
Autoboxing in Wrapper Class
Autoboxing is used to convert primitive data types into corresponding objects
The automatic conversion of primitive data type into its corresponding wrapper class is known as
autoboxing, for example, byte to Byte, char to Character, int to Integer, long to Long, float to Float
boolean to Boolean, double to Double, and short to Short.
we do not need to use the valueOf() method of wrapper classes to convert the primitive into
objects.
Wrapper class Example: Primitive to Wrapper
public class WrapperExample1
{ public static void main(String args[])
{int a=20; //Converting int into Integer
Integer i=Integer.valueOf(a); //converting int into Integer
Integer j=a; //autoboxing, now compiler will write Integer.valueOf(a) internally
System.out.println(a+" "+i+" "+j);
}}
Output: 20 20 20
What is Java I/O?
• Java Input and Output (I/O) is used to process the input and produce
the output based on the input.
• Java uses the concept of stream to make I/O operations fast.
• The java.io package contains all the classes required for input and
output operations.
• File handing in Java can be performed by Java IO API.
Streams
• A stream is a communication channel that a program has with the
outside world.
• It is used to transfer data items in succession.
• A stream is a sequence of data.
• A stream in Java is a path along which data flows (like a river or
pipe along which water flows).
• Every stream has a source and a destination.
Stream Types
• Streams are classified into two basic types:
– Input Stream
– Output Stream

I/O Stream
• An Input/Output (I/O) Stream represents an input source or an output
destination.
Input Vs Output Streams
• If the data flows from a source into a program, it is called as an input
stream.
• If the data flows from a program to a destination (aka sink or target), it
is
called as an output stream.
• Input streams can flow from the keyboard or from a file
– System.in is an input stream that connects to the keyboard.
e.g. Scanner scan= new Scanner(System.in);
• Output streams can flow to a screen or to a file
– System.out is an output stream that connects to the screen.
e.g. System.out.println(“Output Stream”);
Java Stream Classes
• The java.io package contains a large number of stream classes that provide
capabilities for processing all types of data.
• These classes may be categorized into two groups based on the data type on
which they operate.
– Byte stream classes
– Character stream classes

Byte Stream Classes Vs Character Stream Classes


• Byte Stream Classes
– provide support for handling i/o operations on bytes.
• Character Stream Classes
– provide support for managing i/o operations on characters.
Byte Stream Classes
Byte stream is defined by using two abstract class at the top of hierarchy, they are
InputStream and OutputStream.These two abstract classes have several concrete
classes that handle various devices such as disk files, network connection etc.

Byte stream classification


Some important Byte stream classes are:

Stream class Description


BufferedInputStream Used for Buffered Input Stream.
BufferedOutputStream Used for Buffered Output Stream.
DataInputStream Contains method for reading java standard datatype
DataOutputStream An output stream that contain method for writing java
standard data type
FileInputStream Input stream that reads from a file
FileOutputStream Output stream that write to a file.
InputStream Abstract class that describe stream input.
OutputStream Abstract class that describe stream output.
PrintStream Output Stream that contain print() and println() method
Java DataOutputStream Class
Java DataOutputStream class allows an application to write primitive Java data
types to the output stream in a machine-independent way.Java application
generally uses the data output stream to write data that can later be read by a
data input stream.
Java DataOutputStream class methods
Method Description
int size() It is used to return the number of bytes written to the data output stream
void write(int b) It is used to write the specified byte to the underlying output
stream.
void write(byte[] b, int off, int len) It is used to write len bytes of data to the output
stream.
void writeBoolean(boolean v) It is used to write Boolean to the output stream as a
1-byte value.
void writeChar(int v) It is used to write char to the output stream as a 2-byte
value.
void writeChars(String s) It is used to write string to the output stream as a
sequence of characters.
void writeByte(int v) It is used to write a byte to the output stream as a 1-byte
value.
void writeBytes(String s) It is used to write string to the output stream as a
sequence of bytes.
void writeInt(int v) It is used to write an int to the output stream
void writeShort(int v) It is used to write a short to the output stream.
void writeShort(int v) It is used to write a short to the output stream.
void writeLong(long v) It is used to write a long to the output stream.
void writeUTF(String str) It is used to write a string to the output stream using
UTF-8 encoding in portable manner.
void flush() It is used to flushes the data output stream.
Example of DataOutputStream class
In this example, we are writing the data to a text file testout.txt using
DataOutputStream class.
import java.io.*; Output:
public class OutputExample Succcess...
{ testout.txt:
public static void main(String[] args) throws IOException A
{
FileOutputStream file = new FileOutputStream(D:\\testout.txt);
DataOutputStream data = new DataOutputStream(file);
data.writeInt(65);
data.flush();
data.close();
System.out.println("Succcess...");
}
}
Java DataInputStream Class
Java DataInputStream class allows an application to read primitive data from the
input stream in a machine-independent way.Java application generally uses the
data output stream to write data that can later be read by a data input stream.
public class DataInputStream extends FilterInputStream implements DataInput
Java DataInputStream class Methods
Method Description
int read(byte[] b) It is used to read the number of bytes from the input stream.
int read(byte[] b, int off, int len) It is used to read len bytes of data from the input
stream.
int readInt() It is used to read input bytes and return an int value.
byte readByte() It is used to read and return the one input byte.
char readChar() It is used to read two input bytes and returns a char value.
double readDouble() It is used to read eight input bytes and returns a double
value.
boolean readBoolean() It is used to read one input byte and return true if byte is
non zero, false if byte is zero.
int skipBytes(int x) It is used to skip over x bytes of data from the input stream.
String readUTF() It is used to read a string that has been encoded using the UTF-8
format.
void readFully(byte[] b) It is used to read bytes from the input stream and store
them into the buffer array.
void readFully(byte[] b, int off, int len) It is used to read len bytes from the input
stream.
FileInputStream Class
• FileInputStream is useful to read data from a file in the form of
sequence of bytes.
• FileInputStream is meant for reading streams of raw bytes
such as image data.
• Note:
For reading streams of characters, consider
using FileReader.
FileInputStream Class Constructors
and its Description
• FileInputStream(File file)
– Creates an input file stream to read from the specified
File object.
• FileInputStream(FileDescriptor fd)
– Creates an input file stream to read from the specified
file descriptor.
• FileInputStream(String name)
– Creates an input file stream to read from a file with the
specified name.
Character Stream Classes
• Character stream classes are used to read characters from the source and
write characters to destination.
• Character stream classes are in divided in two groups:
– Reader Classes - These classes are subclasses of an abstract class, Reader and
they are used to read characters from a source (file, memory or console).
– Writer Classes - These classes are subclasses of an abstract class, Writer and
they used to write characters to a destination (file, memory or console).
FileReader Class
• Using ‘FileReader’ class, we can read contents of a file.
• Using this class, we can read the contents as a stream of characters.
• Signature:
public class FileReader extends InputStreamReader

How to Read from a File?


• Create an object of FileReader class.
• Start reading from a file using read() method.
– This method reads character by character from the file.
– Actually it returns an int which contains the char value
– After the reading is completed, it returns a ‘-1’.
• After reading is completed, we should always close the ‘FileReader’ using
close() method call.
Writer Class
• Writer class and its subclasses are used to write characters
to a file, memory or console.
• Writer is an abstract class and hence we can’t create its
object but we can use its subclasses for writing characters to
the output stream.

You might also like