0% found this document useful (0 votes)
50 views19 pages

Input/Output (Use of Scanner Class)

This document discusses input/output (I/O) streams in Java, including byte streams that handle input and output of raw bytes and character streams that handle text. It introduces the Scanner class, which simplifies accepting user input from the keyboard or a file. Key points covered include the differences between input and output streams, reading and writing files using FileReader/FileWriter, and common methods of the Scanner class like nextInt() and hasNextInt() for getting user input.

Uploaded by

xradha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views19 pages

Input/Output (Use of Scanner Class)

This document discusses input/output (I/O) streams in Java, including byte streams that handle input and output of raw bytes and character streams that handle text. It introduces the Scanner class, which simplifies accepting user input from the keyboard or a file. Key points covered include the differences between input and output streams, reading and writing files using FileReader/FileWriter, and common methods of the Scanner class like nextInt() and hasNextInt() for getting user input.

Uploaded by

xradha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

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 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


System.in

InputStreamReader read = new InputStreamReader(System.in);

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

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 b.length 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 java.util package. It is used for simplifying data input, & tokenizing data. Character.isWhiteSpace(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

You might also like