Input/Output (Use of Scanner Class)
Input/Output (Use of Scanner Class)
Introduction
What
is a Stream? - flow of data - considered as an abstraction Classes related to input / output ? - java.io Source of data - Input Stream Destination of data - Output Stream
Streams of Data
Binary Input
stream
Output
stream
stream
Buffered
Data Source
Program
001101 101000 1000110
Program
001010
1010000
100010
Data Destination
System.out
System.out.println( );
Byte Stream
Methods
of InputStream class
int read( )
Returns an integer representation of the next available byte of input.
int read(byte b[ ])
Reads upto b.length bytes into b & returns actual number of bytes that were read successfully
Cont.
Methods
of OutputStream class
void write(int b)
Writes a single byte to an output stream.
void write(byte b[ ])
stream
File Input
DataInputStream
- Handles reading of data as well as lines of text available( ) - checks if any data is left to read, returns the number of bytes remaining readLine( ) - prints the data on the output terminal
File Output
FileOutputStream
- handles output related to bytes
- to use FileOutputStream we need to create an object of PrintStream Example FileOutputStream out; PrintStream p; out = new FileOutputStream(fileName);
Character Stream
Reader
class
Cont.
void close( )
Closes the input stream. Further read operation attempts will generate an IOException
Writer
Class
void flush( )
Flushs the output stream.
Cont.
void write(char b[ ])
Writes a complete array of characters to the invoking output stream.
Cont.
FileReader
Class Class
FileReader(File fileObj)
FileReader(String fileName)
FileWriter
FileWriter(File fileObj)
FileWriter(String fileName)
BufferedReader
readLine( )
Cont.
PrintWriter
print( )
println( )
Scanner Class
It
is used to define an object to assign a value to variable as primitive type directly without using the wrapper classes. It is available in java.util package. It is used for simplifying data input, & tokenizing data. Character.isWhiteSpace(char c)