--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD - IT)
E-Mail : rupareleducation@gmail.com
Contact No : 7600044051
Java : Unit 3 - Part 2
Que 1:
Stream and its type.
·
Stream is a order sequence of data that
is common characteristics share by the entire Input/Output device.
·
A stream has a source and destination of
data.
·
A stream is an abstract that either
produce or consume information.
·
A stream is link to physical device by
the java input/output stream.
·
For ex. An input stream nay read its
data from keyboard, file or memory buffer.
·
While an output stream nay write a data
to a monitor, file or memory buffer.
·
There are 2 types of streams.
1. Character
stream
2. Byte
stream
·
Character: Character stream allow you to
read and write character and strings. The character is store and retrieves in
human readable character form. And input stream convert byte to character and
output stream convert character to byte.
·
Byte: Byte
stream allow you to read and write binary data in this data are access as a
sequence of bytes.
Following
is a diagram of stream class.
According to above diagram Java Stream
Class Categorized in byte stream as well as character stream and it also
categorize in subcategory according to byte stream and character stream class.
Que 2 :Java - RandomAccessFile
This class is
used for reading and writing to random access file. A random access file
behaves like a large array of bytes. There is a cursor
implied to the array called file pointer, by moving the
cursor we do the read write operations. If end-of-file is reached before the
desired number of byte has been read than EOFException is thrown. It
is a type of IOException.
RandomAccessFile class is
part of Java IO. While creating the instance of RandomAccessFile in java, we need to
provide the mode to open the file. For example, to open the file for read only
mode we have to use “r” and for read-write operations we have to use “rw”.
Using file pointer, we can
read or write data from random access file at any position. To get the current
file pointer, you can call getFilePointer () method and to set the
file pointer index, you can call seek (int i) method.
Que 3 : Reading and Writing through
Character Stream Classes
One
of the limitations of byte stream classes is that it can handle only 8-bit
bytes and cannot work directly with Unicode characters. To overcome this
limitation, character stream classes have been introduced in java.io
package to match the byte stream classes. The character stream classes support
16-bit Unicode characters, performing operations on characters, character
arrays, or strings, reading or writing buffer at a time. Character stream
classes are divided into two stream classes namely, Reader class and Writer
class.
Que 4 : FileWriter Class
Java
FileWriter class is used to write character-oriented data to a file. It is character-oriented class which is used for file handling
in java.
Unlike
FileOutputStream class, you don't need to convert string into byte array because it provides method to write string directly.
import java.io.*;
class P43
{
public static void
main(String args[])
{
try
{
FileWriter
fw = new FileWriter(args[0]);
for(int
i=1;i<=10;i++)
{
fw.write("\n"+i);
}
fw.close();
}
catch(Exception
e){}
}
}
According
to above example we can write file data which is define in command line
argument and store all the date in particular file using File Writer class.
Que 5 : FileReader Class
Java
FileReader class is used to read data from the file. It returns data in byte
format like FileInputStream class.
import
java.io.*;
class
P44
{
public static void main(String
args[])
{
try
{
FileReader
fr = new FileReader(args[0]);
int i;
while((i=fr.read())
!= -1)
{
System.out.print((char)i);
}
fr.close();
}
catch(Exception e){}
}
}
According
to above example we can read file data which is define in command line argument
and display up to end of file.
Que 6 : Java BufferedWriter Class
Java
BufferedWriter class is used to provide buffering for Writer instances. It
makes the performance fast. It inherits Writer class. The buffering characters are used for providing the
efficient writing of single arrays, characters, and strings.
import
java.io.*;
class
P45
{
public static void main(String
args[])
{
try
{
FileWriter
fw = new FileWriter(args[0]);
BufferedWriter
bw = new BufferedWriter(fw);
for(int
i=1;i<=10;i++)
{
bw.write("\n"+i);
}
bw.close();
}
catch(Exception e){}
}
}
According
to above example we can write file data which is define in command line
argument and store all the date in particular file using BufferedWriter class.
Que 7 : Java BufferedReader Class
Java
BufferedReader class is used to read the text from a character-based input
stream. It can be used to read data line by line by readLine () method. It
makes the performance fast. It inherits Reader class.
import java.io.*;
class P46
{
public static void
main(String args[])
{
try
{
FileReader
fr = new FileReader(args[0]);
BufferedReader
br = new BufferedReader(fr);
int
i;
while((i=br.read())
!= -1)
{
System.out.print((char)i);
}
br.close();
}
catch(Exception
e){}
}
}
According to above example we can read file data which
is define in command line argument and display up to end of file using BufferdReader
class.
Que 8 : Byte Streams
Java byte streams are used to perform input and output of 8-bit
bytes. Though there are many classes related to byte streams but the most
frequently used classes are, FileInputStream and FileOutputStream.
Following is an example which makes use of these two classes to copy an input
file into an output file
Que 9: InputStream
The Java.io.InputStream class is the superclass of all classes
representing an input stream of bytes. Applications that need to define a
subclass of InputStream must always provide a method that returns the next byte
of input.
Que 10 : FileInputStream
The Java.io.FileInputStream class obtains input bytes
from a file in a file system. What files are available depends on the host
environment. Following are the important points about FileInputStream −
·
This
class is meant for reading streams of raw bytes such as image data.
·
For
reading streams of characters, use FileReader.
import java.io.*;
class P47
{
public
static void main(String args[]) throws IOException
{
InputStreamReader ir = new
InputStreamReader(System.in);
BufferedReader
br = new BufferedReader(ir);
System.out.print("Enter
Name :");
String
s = br.readLine();
System.out.println("Name
is :" + s+" and Length of String is :"+s.length());
}
}
According
to above example we can write file data using InputStreamReader which is define
in command line argument and store all the date in particular file using
BufferedReader class.
Que 11 : BufferedInputStream
The Java.io.BufferedInputStream class adds functionality
to another input stream, the ability to buffer the input and to support the
mark and reset methods. Following are the important points about
BufferedInputStream −
·
When
the BufferedInputStream is created, an internal buffer array is created.
·
As
bytes from the stream are read or skipped, the internal buffer is refilled as
necessary from the contained input stream, many bytes at a time.
import java.io.*;
class P47
{
public
static void main(String args[]) throws IOException
{
InputStreamReader
ir = new InputStreamReader(System.in);
BufferedReader
br = new BufferedReader(ir);
System.out.print("Enter
Name :");
String
s = br.readLine();
System.out.println("Name
is :" + s+" and Length of String is :"+s.length());
}
}
According
to above example we can write file data using InputStreamReader which is define
in command line argument and store all the date in particular file using
BufferedReader class.
Que 12 : DataInputStream
The Java.io.DataInputStream class lets an application read
primitive Java data types from an underlying input stream in a
machine-independent way.Following are the important points about
DataInputStream −
·
An
application uses a data output stream to write data that can later be read by a
data input stream.
·
DataInputStream
is not necessarily safe for multithreaded access. Thread safety is optional and
is the responsibility of users of methods in this class.
import java.io.*;
class P55
{
public
static void main(String args[]) throws IOException
{
FileInputStream
fs = new FileInputStream(args[0]);
DataInputStream
ds = new DataInputStream(fs);
int
i;
while((i=ds.read())
!= -1)
{
System.out.println(i);
}
ds.close();
}
}
Que 13 : OutputStream
The Java.io.OutputStream class is the superclass of all
classes representing an output stream of bytes. An output stream accepts output
bytes and sends them to some sink. Applications that need to define a subclass
of OutputStream must always provide at least a method that writes one byte of
output.
Que 14 : FileOutputStream
The Java.io.FileOutputStream class is an output stream
for writing data to a File or to a FileDescriptor. Following are
the important points about FileOutputStream −
·
This
class is meant for writing streams of raw bytes such as image data.
·
For
writing streams of characters, use FileWriter.
import
java.io.*;
public
class P56
{
public
static void main(String[] args)
{
String
s = "Ruparel Education";
try
{
//For
Writing
OutputStream
os = new FileOutputStream(args[0]);
OutputStreamWriter
writer = new OutputStreamWriter(os);
//For
Reading
FileInputStream
in = new FileInputStream(args[0]);
writer.write(s,0,7);
//Flush into
the file
writer.flush();
for
(int i = 0; i <7; i++)
{
System.out.print(""
+ (char) in.read());
}
}
catch (Exception e)
{
System.out.println
(e);
}
}
}
Que 15 : BufferedOutputStream
The Java.io.BufferedOutputStream class implements a buffered
output stream. By setting up such an output stream, an application can write
bytes to the underlying output stream without necessarily causing a call to the
underlying system for each byte written.
Que 16 : DataOutputStream
The Java.io.DataOutputStream class lets an application write
primitive Java data types to an output stream in a portable way. An application
can then use a data input stream to read the data back in.
Que
17 : StreamTokenizer
·
The StreamTokenizer is used to breaks
up the Reading stream into
tokens that are delimited by sets of characters.
·
The
next token is obtained from the Reading stream by calling nextToken( ). It returns the type of
token.
·
StreamTokenizer
defines four int constants : TT
EOF, TT EOL, TT NUMBER, and TT WORD.
·
There
are three instance variables : nval, sval, and ttype. The nval is
a public double used
to hold the values of numbers as they are recognized.
·
The sval is a public string used to hold the value of
any words as they are recognized. The ttype is a public int indicating the type of token that has
just been read by the nextToken( ) method.
·
If
the token is a word, ttype
equals TT WORD. If the
token is a number, ttype
equals TT NUMBER. If the
token is a single character,
ttype contains its value. If an end
of linecondition has been encountered, ttype equals TT EOL. If the end of the stream has been
encountered, ttype equals TT EOF.
import
java.io.*;
public
class P57
{
public static void main(String[]
args) throws InterruptedException, FileNotFoundException,
IOException
{
FileReader reader = new
FileReader(args[0]);
BufferedReader
bufferread = new BufferedReader(reader);
StreamTokenizer token =
new StreamTokenizer(bufferread);
int t;
while ((t =
token.nextToken()) != StreamTokenizer.TT_EOF)
{
switch (t)
{
case
StreamTokenizer.TT_NUMBER:
System.out.println("Number
: " + token.nval);
break;
case
StreamTokenizer.TT_WORD:
System.out.println("Word
: " + token.sval);
break;
}
}
}
}
According
to above example StreamTokenizer Each token are separated by its type that may
be Number Type , Word Type etc…
Question:
Piped Streams in Java
Answer:
Pipes in IO provides a link between
two threads running in JVM at the same time. So, Pipes are used both as source
or destination.
·
PipedInputStream is also piped with
PipedOutputStream. So, data can be written using PipedOutputStream and can be
written using PipedInputStream. But, using both threads at the same time will
create a deadlock for the threads.
·
A pipe is said to be broken if a
thread that was providing data bytes to the connected piped output stream is no
longer alive
Question
: Input Sream Reader Class
Answer : An
InputStreamReader is a bridge from byte streams to
character streams. It reads bytes and decodes them into characters using a
specified charset. The charset that it uses may be specified by name or may be
given explicitly, or the platform’s default charset may be accepted.
Question : Output Steram Writer Class
Answer : OutputStreamWriter
is a class which is used
to convert character stream to byte stream, the characters are encoded into
byte using a specified charset. write() method calls the encoding converter
which converts the character into bytes. The resulting bytes are then
accumulated in a buffer before being written into the underlying output stream.
The characters passed to write() methods are not buffered. We optimize the
performance of OutputStreamWriter by using it with in a BufferedWriter so that
to avoid frequent converter invocation.
Question : Object Input Stream class in
Java
Answer : The Java.io.ObjectInputStream class
deserializes primitive data and objects previously written using an
ObjectOutputStream. Following are the important points about ObjectInputStream
·
It is used to recover those objects previously serialized. It ensures
that the types of all objects in the graph created from the stream match the
classes present in the Java Virtual Machine.
·
Classes are loaded as required using the standard mechanisms.
Question : Object Output Stream class in
Java
Answer : The Java.io.ObjectOutputStream class writes
primitive data types and graphs of Java objects to an OutputStream.The objects
can be read (reconstituted) using an ObjectInputStream. Persistent
storage of objects can be accomplished by using a file for the stream.
:: Best Of Luck ::
Please share and comment it... Thank you