Java I/O
Chittaranjan Pradhan
Web Technology 13 I/O Basics
Stream
Java I/O Reading Console Input
Reading with
[Link] class
Writing Console
Output
PrintWriter Class
Reading & Writing
Files
Character-based Reading &
Writing
Chittaranjan Pradhan
School of Computer Engineering,
KIIT University
13.1
Java I/O
I/O Basics
Chittaranjan Pradhan
I/O Basics
Stream
Reading Console Input
I/O Basics Reading with
[Link] class
Writing Console
• Java I/O is used to process the input and produce the Output
output based on the input PrintWriter Class
Reading & Writing
• Java uses the concept of stream to make I/O operations Files
Character-based Reading &
• [Link] package contains all the classes required for input Writing
and output operations
• Two important methods are read() and write()
13.2
Java I/O
Stream
Chittaranjan Pradhan
Stream
I/O Basics
• A stream is an abstraction that either produces or Stream
Reading Console Input
consumes information. It is linked to a physical device by
Reading with
the Java IO system [Link] class
Writing Console
• A stream is a sequence of data. It is composed of bytes Output
• [Link] package defines a class called System, which PrintWriter Class
Reading & Writing
encapsulates several aspects of the run-time environment Files
Character-based Reading &
• [Link]: Writing
• It refers to the standard output stream
• [Link]:
• It refers to the standard input stream
• [Link]:
• It refers to the standard error stream
[Link] is an object of type InputStream. [Link] and
[Link] are objects of type PrintStream
13.3
Java I/O
Stream...
Chittaranjan Pradhan
I/O Basics
Byte Stream Classes Stream
Reading Console Input
• These are defined by using two class hierarchies at the Reading with
top: InputStream and OutputStream [Link] class
Writing Console
• To use stream classes, you must import [Link] Output
PrintWriter Class
Reading & Writing
• OutputStream: Files
Character-based Reading &
• It is an abstract class. It is the superclass of all classes Writing
representing an output stream of bytes
• It is used to write data to a destination
• InputStream:
• It is an abstract class. It is the superclass of all classes
representing an input stream of bytes
• It is used to read data from a source
13.4
Java I/O
Stream...
Chittaranjan Pradhan
Character Stream Classes I/O Basics
Stream
• These are defined by using two class hierarchies at the Reading Console Input
top: Reader and Writer Reading with
[Link] class
• These classes handle Unicode character streams Writing Console
Output
PrintWriter Class
• Reader: Reading & Writing
Files
• It is the abstract class that describes character stream input Character-based Reading &
Writing
• Reader class contains methods that are identical to those
available in InputStream class, except Reader is designed
to handle characters
• Writer:
• It is the abstract class that describes character stream
output
• It provides support for all output operations by defining
methods that are identical to those in OutputStream class
13.5
Java I/O
Reading Console Input
Chittaranjan Pradhan
Reading Console Input
I/O Basics
• Console input is accomplished by reading from [Link]. Stream
To obtain a character based stream that is attached to Reading Console Input
Reading with
console, wrap [Link] in a BufferedReader object [Link] class
• BufferedReader br=new BufferedReader(new Writing Console
Output
InputStreamReader([Link])); PrintWriter Class
Reading & Writing
Files
Character-based Reading &
Reading Characters Writing
• int read() throws IOException
• char ch=(char)[Link]();
Reading Strings
• String readLine() throws IOException
• String str=[Link]();
13.6
Java I/O
Reading Console Input...
Chittaranjan Pradhan
Reading Integers
I/O Basics
• int [Link]([Link]());
Stream
• int item=[Link]([Link]()); Reading Console Input
Reading with
[Link] class
Reading Other Types of Values Writing Console
Output
PrintWriter Class
• [Link]([Link]()) Reading & Writing
Files
Character-based Reading &
Writing
• [Link]([Link]())
• [Link]([Link]())
• [Link]([Link]())
• [Link]([Link]())
• [Link]([Link]())
13.7
Java I/O
Reading with [Link] class
Chittaranjan Pradhan
I/O Basics
Stream
Reading with [Link] class Reading Console Input
Reading with
[Link] class
• Scanner sc=new Scanner([Link]);
Writing Console
• When the Scanner class receives input, it breaks it into Output
PrintWriter Class
several pieces, called tokens
Reading & Writing
• String str=[Link](); Files
Character-based Reading &
String str=[Link](); Writing
char ch=[Link](0);
int item=[Link]();
float bal=[Link]();
long a=[Link]();
long b=[Link]();
13.8
Java I/O
Writing Console Output
Chittaranjan Pradhan
I/O Basics
Stream
Reading Console Input
Reading with
Writing Console Output [Link] class
Writing Console
Output
• Console output is accomplished by print() and println().
PrintWriter Class
These methods are defined by PrintStream class Reading & Writing
Files
• PrintStream is an output stream derived from Character-based Reading &
Writing
OutputStream. It also implements write()
• int b=’P’;
[Link](b);
13.9
Java I/O
PrintWriter Class
Chittaranjan Pradhan
I/O Basics
Stream
PrintWriter Class Reading Console Input
Reading with
• PrintWriter is one of the character based classes [Link] class
Writing Console
• PrintWriter(OutputStream outputStream, boolean Output
flushOnNewline) PrintWriter Class
Reading & Writing
• PrintWriter supports print() and println() methods for all Files
types including Object Character-based Reading &
Writing
• To write to the console by using PrintWriter, specify
[Link] for the output stream and flush the stream
after each newline
• PrintWriter pw=new PrintWriter([Link], true);
[Link]("Bye");
13.10
Java I/O
Reading & Writing Files
Chittaranjan Pradhan
I/O Basics
Reading & Writing Files Stream
Reading Console Input
• Java files are byte-oriented Reading with
[Link] class
• Java allows wrapping of a byte-oriented file stream within Writing Console
a character-based object Output
PrintWriter Class
• Most used stream classes are: FileInputStream and
Reading & Writing
FileOutputStream Files
Character-based Reading &
Writing
• FileInputStream(String fname) throws
FileNotFoundException
FileOutputStream(String fname) throws
FileNotFoundException
• When an output file is opened, any preexisting file by the
same name is destroyed
13.11
Java I/O
Reading & Writing Files...
Chittaranjan Pradhan
I/O Basics
Reading & Writing Files... Stream
Reading Console Input
• After the work is over, the file should be closed by using Reading with
close() method [Link] class
Writing Console
void close() throws IOException Output
PrintWriter Class
• To read from a file, use Reading & Writing
Files
int read() throws IOException Character-based Reading &
Writing
Each time the read() is called, it reads a single byte from
the file and returns the byte as an integer value
• To write to a file, use
void write(int byteval) throws IOException
Though byteval is declared as an integer, only the
low-order 8 bits are written to the file
13.12
Java I/O
Reading & Writing Files...
Chittaranjan Pradhan
I/O Basics
Writing Files
Stream
DataInputStream class is used to read data from keyboard Reading Console Input
DataInputStream dis = new DataInputStream([Link]); Reading with
[Link] class
Writing Console
Output
PrintWriter Class
Reading & Writing
Files
Character-based Reading &
Writing
13.13
Java I/O
Reading & Writing Files...
Chittaranjan Pradhan
Reading Files
I/O Basics
Stream
Reading Console Input
Reading with
[Link] class
Writing Console
Output
PrintWriter Class
Reading & Writing
Files
Character-based Reading &
Writing
13.14
Java I/O
Reading & Writing Files...
Chittaranjan Pradhan
Copying Files
I/O Basics
Stream
Reading Console Input
Reading with
[Link] class
Writing Console
Output
PrintWriter Class
Reading & Writing
Files
Character-based Reading &
Writing
13.15
Java I/O
Character-based Reading & Writing
Chittaranjan Pradhan
I/O Basics
Writing Files using FileWriter Stream
FileWriter is useful to create file by writing characters into it Reading Console Input
Reading with
[Link] class
Writing Console
Output
PrintWriter Class
Reading & Writing
Files
Character-based Reading &
Writing
13.16
Java I/O
Character-based Reading & Writing...
Chittaranjan Pradhan
Reading Files using FileReader
FileReader is useful to read data in the form of characters I/O Basics
Stream
Reading Console Input
Reading with
[Link] class
Writing Console
Output
PrintWriter Class
Reading & Writing
Files
Character-based Reading &
Writing
13.17