Monday, November 28, 2016

input and output stream - input and output (I/O)


          Functions Input & Output  are used to process produce

In java input and output are used to process some input and produce output on the base of given input. When we talk about input and output the java provide the stream concepts which is better for fast performance of input and output.input and output (I/O)

Stream
Basically stream represent the flow of water but in programing stream means sequence of data or flow of data. Stream is composed of bytes.input and output (I/O)

input and output stream -  input and output (I/O)

Types of Stream
There are two types of stream in java
o   Byte Stream
o   Character Stream

input and output (I/O)

Byte Stream
The byte stream handles the input and output in bytes. There are two types of byte stream

Types of Byte Stream
There are two types of byte stream in java
o   Input Stream
o   Output Stream

Input Stream

In java input stream is an abstract class of stream which is used to read the data from source. The source may be a file, array, socket, etc. Input Stream is further categorized into the following
  o   File Input Stream
  o   Sequence Input Stream
  o   Filter Input Stream
·         Buffered Input Stream
·         Data input Stream

Output Stream
In java output stream is an abstract class of stream which is used to write the data to a destination. The destination may be a file, array, socket, etc. Output Stream is further categorized into the following
  o   File Output Stream
  o   Byte Array Output Stream
  o   Filter Output Stream
·         Buffered Output Stream
·         Data Output Stream

File input Stream and File Output Stream
In java file input stream and file output Stream is used for file handling, through these two classes you can read and write the data.

input and output stream -  input and output (I/O)

Example of File Output and Input Stream


Output Input Strem

Byte Array Output Stream
The byte array output stream is used to write the data into multiple files. When you want to write same data in different files then Byte Array Stream Output is best.

Sequence Input Stream
The Sequence Input stream is used to read the data from multiple files. When you want to read data from different files then Sequence Input Stream is best.input and output (I/O)

Example of Sequence Input Stream

input output java

           Buffer Input and Output Stream
The buffer input stream and buffer output stream, these both classes are used to internal buffer to read and write the data respectively. This internal buffer make performance fast.

Example

input output stream buffer

      
            Data Input and Data Output Stream in Java
Data output stream class is used to write primitive datatype data into the file. It provides the different types of method to write respected primitive data. Same way the data input stream class is used to read the primitive data from the file.
input and output (I/O)

Example

inputOutputStream


Now above we discuss all byte stream mentioned topics now we are going to discuss the character stream
input and output stream -  input and output (I/O)

          

     Character Stream  Input and Output in Java
The character stream handles the input and output direct in characters. There are two types of character stream.
  o   File writer
  o   File reader

File Reader and File Writer
In java file reader and filer writer classes are used to read and write the data from files respectively
input and output (I/O)
Example

inputFileReadWrite
input and output stream -  input and output (I/O)


 Java Read Input from Keyboard
When you want to user interaction with your application then you need to process some inputs which will be given by users during running the applications. So, for this purpose java provides some built-in classes that will take input from users. Following are the classes which are used to take inputs from users
o   Input Stream Reader
o   Console
o   Scanner
o   Data Input Stream

Input Stream Reader
Input Stream reader class is used to read the data from keyboard for this Input Stream reader first collect the stream then convert these stream in bytes using Buffered Reader class.

 Example:

inputRead
input and output stream -  input and output (I/O)

       Console  Java Input and Output

Console class is used to read the data from keyboard but console is not support by IDE for this you have to use CMD to aware with Console environment.

        Scanner
In java the Scanner is most popular class to read the data from keyboards. It has primitive datatype method to read the respected primitive datatype value

Scanner Class Some Common Methods 
Method
Description
String next()
it returns the next token from the scanner.
String nextLine()
it moves the scanner position to the next line and returns the value as a string.
publibytenextByte()
it scans the next token as a byte.
short nextShort()
it scans the next token as a short value.
intnextInt()
it scans the next token as an int value.
long nextLong()
it scans the next token as a long value.
float nextFloat()
it scans the next token as a float value.
double nextDouble()
it scans the next token as a double value.

Example:


ScanerClass
input and output stream -  input and output (I/O)

Data Input Stream
This is also used to read the data from keyboard, for this class you have to use checked exception otherwise you will get compile time error.

Example:

inputDataStream


                                        input and output stream -  input and output (I/O)












Sunday, November 27, 2016

Modifiers Java -modifiers java Exception Keywords

Modifiers Java -modifiers java Exception Keywords

Modifiers Java -modifiers java Exception Keywords

       Modifiers keyword used 

In java modifiers are keyword which are used to change the current definition of respected element. There are two categories of Java Modifiers
o   Access Modifier
o   Non-Access Modifier.
        modifiers java Exception Keywords
Access Modifier
Those modifiers which showing the scope of the element are known as Access Modifier. There are four types of Access Modifier.
  o   Default: It’s scope is within respected package except methods and data members of super class
  o   Public: it’s scope is within all over the packages, classes, interfaces etc.
  o   Private: it’s scope is within class 
  o   Protected: It’s scope is within respected package

Non-Access Modifier
Those modifiers which are but definition scope but change the functionality of the element are known as Non-Access Modifiers. Following are the type of Non-Access Modifier
  o   Static: used for create method and variable static
  o   Final: used for create class, method and variable final
  o   Abstract: used for creating abstract class and abstract method
  o   Synchronized: used for threads
  o   Volatile:  used for threads.

modifiers java Exception Keywords

Access Modifier
within class
within package
outside package by subclass only
outside package
Private
Y
N
N
N
Default
Y
Y
N
N
Protected
Y
Y
Y
N
Public
Y
Y
Y
Y






  Exception Handling
Exception handling is most powerful mechanism in java to control the exception which are occurs during program execution. Basically exception is one kind of interrupt which disturb the flow of program and exception handling remove these disturbances and maintain the flow of the programs.

Types of exception

There are three types of exception in which are the following Java:

  o   Checked Exception: those exceptions which are checked at compile time are known as Checked Exceptions. In other words, the compiler for fully say that to compile this piece of code you must enclosed this piece of code within any exception handling tools. Some of possible checked exceptions are listed below
1.       IOException
2.       SQLException
3.       DataAccessException
4.       ClassNotFoundException
5.       InvocationTargetException
6.       MalformedURLException

  o   Un-Checked Exception: those exceptions which are checked at runtime are known as Un Checked Exceptions. These exceptions are caused due to bad programming or poor programming. these exceptions are occurring when user interacting with your application, it may be caused of wrong inputs or invalid data etc. some of possible Un-Checked exceptions are listed below:
1.       NullPointerException
2.       ArrayIndexOutOfBoundsException
3.       ArithmeticException
4.       IllegalArgumentException
5.       NumberFormatException

  o   Error: when the expected outcome is different than it is known as error. So, in programming mostly errors are uncovering because we don’t know the occurring of errors.  Error is not an exception. When your programs are failed to give expected result this is error.

       Java Exception Handling Keywords
In Java there are five keywords are used for Checked and Un-Checked exception handling
  o   try
  o   catch
  o   finally
  o   throw
  o   throws

modifiers java Exception Keywords

       Try Catch block
when you write any piece of program and you think that their might be a chances of error occurring then to prevent this type of errors you have to use try catch block. The try block will execute your program, if any exception occur the catch block will catch that exception and throw it at runtime.

Example without try catch block


withoutTryCatchBlock

In above example the JVM first check any exception occur or not? If occur, then it checks any exception handler is available or not? If no any exception handler is available then JVM will handle its own ways, otherwise the flow of the program will be maintained by the respected exception handler. Now as we see on above image that exception will occur at line number five, in above example we divide 10 by 0 so in programing division by zero is not perform that’s why it’s trough’s the runtime exception and stop the execution of the program. 

Example with try catch block

TryCatchBlock
modifiers java Exception Keywords
Now in above example we enclosed the ambiguity piece of code into try catch block, if there is any exception then catch block will catch the exception. As we see the exception is occurred but flow of program is not stop because of try catch exception handler.modifiers java Exception Keywords

You can use multiple catch block for example if you have a doubt that there may be an Arithmetic Exception will occur or may be an Array Outs of Bounds exception will occur then you can write multiple catch blocks surrounded by try block

try{
                // code for execution
} catch (ArithmeticException e) {
                // message when exception occur
} catch (ArrayOutOfBoundsException e) {
                // message when exception occur
}

Finally, block
Finally, block is actually not handling the exception. This block always executes either the exception occurs or not. This block is mainly used when you want to close data base connection or you want to leave a message that user always get this message. So, to handle these type of thing the finally block will execute

Example

FinallyBlock
modifiers java Exception Keywords
Now in above example as we notice the finally block is executing either any exception occurs or not. Now in each try block you can put more than one catch block but finally block will be used only once.


        Throw Exception
In java the throw keyword is an explicitly throw an exception for example if we have any piece of code and we already know that in this piece of code the chances of exception are more then we have to use throw exception.

Example

ThroughHandler
modifiers java Exception Keywords
Now in above program it generates a throw exception and stop the execution of the program

Throws Exception

In java the throws keyword is used for exception handling, this throws exception handling is used for to handle the check exception. This exception handler is helps the programmer to handle the normal flow of the program after any exception occur. If any unchecked exception is occurred than it’s your fault and you have to control it and throws exception handler is not responsible for it.

Example

ThrowHandler



modifiers java Exception Keywords