Wednesday, February 28, 2018

Character Stream Class in Java for B.C.A., M.C.A. and all IT Students


--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD-IT)
Contact No : 7600044051
E-Mail : rupareleducation@gmail.com



Programming with JAVA


Byte Stream Classes in java


Byte stream classes are used to perform reading and writing of 8-bit bytes. Streams being unidirectional in nature can transfer bytes in one direction only, that is, either reading data from the source into a program or writing data from a program to the destination.
Therefore, Java further divides byte stream classes into two classes, namely, InputStream class and OutputStrearn class.
The subclasses of InputStrearn class contain methods to support input and the subclasses of OutputStrearn class contain output related methods.

Input Stream Classes

Java's input stream classes are used to read 8-bit bytes from the stream. The InputStrearn class is the superclass for all byte-oriented input stream classes. All the methods of this class throw an IOException. Being an abstract class, the InputStrearn class cannot be instantiated hence, its subclasses are used. Some of these are listed in Table
Table Input Stream Classes
Class
Description
FileInputStream
contains methods to read bytes from a file
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.
BufferedInputStream
contains methods to read bytes from the buffer (memory area)
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.
DataInputStream
contains methods to read Java primitive data types
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.



Output Stream Class

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.

Method & Description
This method closes this output stream and releases any system resources associated with this stream.
This method flushes this output stream and forces any buffered output bytes to be written out.
This method writes b.length bytes from the specified byte array to this output stream.
This method writes len bytes from the specified byte array starting at offset off to this output stream.
This method writes the specified byte to this output stream.


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.

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.
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.
StringTokenizer
The StringTokenizer class is used to create a parser for String objects. It parses strings according to a set of delimiter characters. It implements the Enumeration interface in order to provide access to the tokens contained within a string. StringTokenizer provides three constructors:
StringTokenizer(String s)
StringTokenizer(String s, String d)
StringTokenizer(String s, String d, boolean t)
s is the input string, d is a set of delimiters to be used in the string parsing and t is a boolean value used to specify whether the delimiter characters should be returned as tokens or not. The default delimiter considered are : space, tab, newline, and carriage-return characters.
The access methods provided by StringTokenizer include the Enumeration methods, hasMoreElements() and nextElement(), hasMoreTokens() and nextToken(), and countTokens()· The countTokens() method returns the number of tokens in the string being parsed.
PipedStream

The PipedInputStream and PipedOutputStream classes can be used to read and write data simultaneously. Both streams are connected with each other using the connect() method of the PipedOutputStream class.

The Java.io.PipedInputStream class is a piped input stream that can be connected to a piped output stream, the piped input stream then provides whatever data bytes are written to the piped output stream.Following are the important points about PipedInputStream −
·        The piped input stream contains a buffer, decoupling read operations from write operations, within limits.
·        Attempting to use both objects from a single thread is not recommended, as it may deadlock the thread.
·        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.

The Java.io.PipedOutputStream class is a piped output stream that can be connected to a piped input stream to create a communications pipe.Following are the important points about PipedOutputStream −
·        The piped output stream is the sending end of the pipe.
·        Attempting to use both objects from a single thread is not recommended as it may deadlock the thread.
·        Data is written to a PipedOutputStream object by one thread and data is read from the connected PipedInputStream by some other thread.
The pipe is said to be broken if a thread that was reading data bytes from the connected piped input stream is no longer alive.
InputStreamReader
The InputStreamReader class of java.io package, 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. Each invocation of one of an InputStreamReader’s read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.

OutputStreamWriter
The Java OutputStreamWriter class (java.io.OutputStreamWriter) is intended to wrap an OutputStream, thereby turning the byte based output stream into a character based Writer.
The Java OutputStreamWriter is useful if you need to write characters to a file, encoded as e.g. UTF-8 or UTF-16. You can then write the characters (char values) to the OutputStreamWriter and it will encode them correctly and write the encoded bytes to the underlying OutputStream.

ObjectInputStream
The Java ObjectInputStream class (java.io.ObjectInputStream) enables you to read Java objects from an InputStream instead of just raw bytes. You wrap an InputStream in a ObjectInputStream and then you can read objects from it. Of course the bytes read must represent a valid, serialized Java object. Otherwise reading objects will fail.
Normally you will use the ObjectInputStream to read objects written (serialized) by a Java ObjectOutputStream . You will see an example of that later.

ObjectOutputStream
The Java ObjectOutputStream class (java.io.ObjectOutputStream) enables you to write Java objects to an OutputStream instead of just raw bytes. You wrap an OutputStream in a ObjectOutputStream and then you can write objects to it.
The Java ObjectOutputStream is often used together with a Java ObjectInputStream. The ObjectOutputStream is used to write the Java objects, and the ObjectInputStream is used to read the objects again. You will see an example of this later.


Best Of Luck 

Please Share and Give your reviews.... Thank you

Sunday, February 25, 2018

Character Stream Class in Java for B.C.A., M.C.A. and all IT Students


--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD - IT)
Contact : 7600044051
E-mail : rupareleducation@gmail.com


Programming With Java
Character Stream

In Java, characters are stored using Unicode conventions. Character stream automatically allows us to read/write data character by character. For example FileReader and FileWriter are character streams used to read from source and write to destination. In Java, characters are stored using Unicode conventions. Character stream is useful when we want to process text files. These text files can be processed character by character. A character size is typically 16 bits.



Java 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.
Following is Method of Java File Write
Method
Description
void write(String text)
It is used to write the string into FileWriter.
void write(char c)
It is used to write the char into FileWriter.
void write(char[] c)
It is used to write char array into FileWriter.
void flush()
It is used to flushes the data of FileWriter.
void close()
It is used to close the FileWriter.

 

Java FileReader Class

Java FileReader class is used to read data from the file. It returns data in byte format like FileInputStream class.
It is character-oriented class which is used for file handling in java.
Following is list of Method
Method
Description
int read()
It is used to return a character in ASCII form. It returns -1 at the end of file.
void close()
It is used to close the FileReader class.


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.
Method
Description
void newLine()
It is used to add a new line by writing a line separator.
void write(int c)
It is used to write a single character.
void write(char[] cbuf, int off, int len)
It is used to write a portion of an array of characters.
void write(String s, int off, int len)
It is used to write a portion of a string.
void flush()
It is used to flushes the input stream.
void close()
It is used to closes the input stream

Java BufferedReader Class
The Java.io.BufferedReader class reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.Following are the important points about BufferedReader −
·        The buffer size may be specified, or the default size may be used.
·        Each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream.
Method
Description

This method closes the stream and releases any system resources associated with it.
This method marks the present position in the stream.

This method tells whether this stream supports the mark() operation, which it does.
This method reads a single character.
This method reads characters into a portion of an array.
This method reads a line of text.
This method tells whether this stream is ready to be read.


Best of Luck

Please Share and Give your reviews.... Thank you

Saturday, February 24, 2018

Unit No : 4 Link List in Data Structure Using C - Language for B.C.A , M.C.A. and all IT Students


--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD - IT)
Contact No : 7600044051
E-Mail  : rupareleducaion@gmail.com

Link List Introduction

One disadvantage of using arrays to store data is that arrays are static structures and therefore cannot be easily extended or reduced to fit the data set. 
Arrays are also expensive to maintain new insertions and deletions. 
So we  consider another data structure called Linked Lists that addresses some of the limitations of arrays.

A linked list is a linear data structure where each element is a separate object.



Each element we will call it a node of a list is comprising of two items - the data and a reference to the next node. The last node has a reference to null. The entry point into a linked list is called the head of the list.

It should be noted that head is not a separate node, but the reference to the first node. If the list is empty then the head is a null reference.
A linked list is a dynamic data structure. The number of nodes in a list is not fixed and can grow and shrink on demand.
Any application which has to deal with an unknown number of objects will need to use a linked list.

Advantages of Linked Lists

·                     They are a dynamic in nature which allocates the memory when required.
·                     Insertion and deletion operations can be easily implemented.
·                     Stacks and queues can be easily executed.
·                     Linked List reduces the access time.

Disadvantages of Linked Lists

·                     The memory is wasted as pointers require extra memory for storage.
·                     No element can be accessed randomly; it has to access each node sequentially.
·                     Reverse Traversing is difficult in linked list.
·                     One disadvantage of a linked list against an array is that it does not allow direct access to the individual elements. If you want to access a particular item then you have to start at the head and follow the references until you get to that item.

Applications of Linked Lists

·                     Linked lists are used to implement stacks, queues, graphs, etc.
·                     Linked lists let you insert elements at the beginning and end of the list.
·                     In Linked Lists we don't need to know the size in advance.

Types of Linked Lists

Singly Linked List : Singly linked lists contain nodes which have a data part as well as an address part i.e. next, which points to the next node in sequence of nodes. The operations we can perform on singly linked lists are insertion, deletion and traversal.


Doubly Linked List : In a doubly linked list, each node contains two links the first link points to the previous node and the next link points to the next node in the sequence.




 Circular Linked List : In the circular linked list the last node of the list contains the address of the first node and forms a circular chain.


Circular Linked List is little more complicated linked data structure. In the circular linked list we can insert elements anywhere in the list whereas in the array we cannot insert element anywhere in the list because it is in the contiguous memory. In the circular linked list the previous element stores the address of the next element and the last element stores the address of the starting element. The elements points to each other in a circular way which forms a circular chain. The circular linked list has a dynamic size which means the memory can be allocated when it is required.

Application of Circular Linked List

The real life application where the circular linked list is used is our Personal Computers, where multiple applications are running. All the running applications are kept in a circular linked list and the OS gives a fixed time slot to all for running. The Operating System keeps on iterating over the linked list until all the applications are completed.
Another example can be Multiplayer games. All the Players are kept in a Circular Linked List and the pointer keeps on moving forward as a player's chance ends.
Circular Linked List can also be used to create Circular Queue. In a Queue we have to keep two pointers, FRONT and REAR in memory all the time, where as in Circular Linked List, only one pointer is required.

Traversal of Linked List :

Traversal a list is the easiest of all the operation on liked list. While Traversing a liked list just keep moving to next node starting with the head and stopping as the null is encountered.

Traversal means movement of Pointer with Top to Bottom and Bottom to top. Traversal help to search, display , count and other linked list Operation it also help to find exact location in Liked List. 

Merge two sorted linked lists


Write a Merge function that takes two lists, each of which is sorted or Unsorted in increasing order, and merges the two together into one list which is in increasing order. Merge should return the new list. The new list should be made by marge together 

For example the nodes of the first two lists.

if the first linked list a is 5->10->15 and the other linked list b is 2->3->20, then Merge should return a pointer to the head node of the merged list 5->2->10->3->15->20.
There are many cases to deal with: either ‘a’ or ‘b’ may be empty, during processing either ‘a’ or ‘b’ may run out first, and finally there’s the problem of starting the result list empty, and building it up while going through ‘a’ and ‘b’.

Reversing of Linked List.
 In the concept of reversing of linked list link should be represent with Null in the first stapes. Then each and every node inserted at Beginning of the node. It means there is only one operation and that is insert at beginning.

Data elements are always inserted at the top of the node and when user select to traversal from top to bottom then node with display in reverse order.

Following is some operation that maintain in Linked List.
1.                 Insert
2.                 Display
3.                 Delete

For example : Insert values in Liked list is 5 - 4 - 1 – 2 – 3  then output will be
Output : 3 – 2 – 1 – 4 – 5 

Best Of Luck 

Please Share and Give your comments.... Thank you