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)
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
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
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
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
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.
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:
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:
input and output stream - input and output (I/O)
No comments:
Post a Comment