Input/Output (Use of Scanner Class)
Introduction
What
is a Stream? - flow of data - considered as an abstraction Classes related to input / output ? - [Link] Source of data - Input Stream Destination of data - Output Stream
Streams of Data
Binary Input
stream consist of binary data
stream
Output
stream
stream
Buffered
Reading Information Into a Program
Data Source
Program
001101 101000 1000110
Writing Information From a Program
Program
001010
1010000
100010
Data Destination
Input / Output Data To Screen
[Link]
InputStreamReader read = new InputStreamReader([Link]);
[Link]
[Link]( );
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 [Link] bytes into b & returns actual number of bytes that were read successfully
int read(byte b[ ], int offset, int length)
Reads upto length bytes into b starting at b[offset], returning 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
Writes a complete array of bytes to an output
void write(byte b[ ],int offset,int len)
Writes a sub-range of len bytes from the array b, beginning at b[offste]
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
int read(char b[])
Reads upto [Link] characters into b & returns the actual number of characters that were successfully read.
int read(char b[], int offset, int len)
Reads upto len characters into b array starting at b[offset], returning the number of characters successfully read.
long skip(long num)
Skips over num characters of input, returning the number of characters actually skipped.
Cont.
void close( )
Closes the input stream. Further read operation attempts will generate an IOException
Writer
Class
void flush( )
Flushs the output stream.
void write(int ch)
Writes a single character.
Cont.
void write(char b[ ])
Writes a complete array of characters to the invoking output stream.
void write(char b[ ], int offset, int len)
Writes a sub-range of len characters from the array b, beginning at b[offset] to the invoking output stream.
void write(String str)
Writes str to the invoking output stream
void write(String str, int offset, int len)
Writes a sub-range of len characters from the array str, beginning at b[offset] 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 [Link] package. It is used for simplifying data input, & tokenizing data. [Link](char c)
Constructors for Scanner Class
Scanner(String
scorce) Scanner(InputStream source) Scanner(File source)
Methods of Scanner Class
nextInt(
) nextFloat( ) close( ) has nextInt( )
Getting I/P O/P using Scanner Class
Reading
User Input Reading from a file Writing to a file - BufferedWriter
- PrintWriter