Вы находитесь на странице: 1из 37

Session 3 B: I/O System Java Programming

1 TCS Confidential
Input/Output System
Java Programming
Session 3 B: I/O System Java Programming
2 TCS Confidential
Input/Output in Java
The Java I/O System
Stream Types
The Byte Stream Hierarchy
Predefined Streams in Java
The File Class
Byte Stream Hierarchy Classes
Session 3 B: I/O System Java Programming
3 TCS Confidential
The Java I/O System
Java
Program
Hard Disk
Memory
Session 3 B: I/O System Java Programming
4 TCS Confidential
The Java I/O System
Streams
Java I/O
Physical Devices
Input Stream
Output Stream
Java
Program
File
Memory
Stream (Java I/O API)
Session 3 B: I/O System Java Programming
5 TCS Confidential
Stream Types
Two types of streams
Byte based
Used to read/write byte
data (0-255)
Character based
Used to read/write textual,
Unicode data
Session 3 B: I/O System Java Programming
6 TCS Confidential
Byte Streams
Main abstract classes
InputStream
OutputStream
Concrete stream classes for
handling various devices (e.g.
disk files, memory buffers)
Session 3 B: I/O System Java Programming
7 TCS Confidential
Character Streams
Main abstract classes
Reader
Writer
Concrete subclasses
implements required methods
Session 3 B: I/O System Java Programming
8 TCS Confidential
Abstract Classes
Stream Types Java I/O
Streams
Byte Based
Character Based
InputStream
OutputStream
Reader
Writer
Session 3 B: I/O System Java Programming
9 TCS Confidential
The Byte Stream Hierarchy
BufferedOutputStream
PrintStream
Object
InputStream OutputStream
FileInputStream
FilterInputStream
ByteArrayInputStream
SequenceInputStream
PipedInputStream
FileOutputStream
FilterOutputStream
ByteArrayOutputStream
PipedOutputStream
BufferedInputStream
PushbackInputStream
Session 3 B: I/O System Java Programming
10 TCS Confidential
Java I/O Methods
Commonly Used Methods
InputStream
read(), close()
OutputStream
write(), flush(), close()
Session 3 B: I/O System Java Programming
11 TCS Confidential
Selecting Stream Class
For best performance, use
the most specific stream
class possible
For file read/write use
FileInputStream/
FileOutputStream
Session 3 B: I/O System Java Programming
12 TCS Confidential
Predefined Streams in Java
java.lang.Systemclass
Systemcontains 3 predefined
public and static stream
variables, in, out, and err.
System.in: is of type
InputStream
System.out, System.err:
are of type PrintStream
Session 3 B: I/O System Java Programming
13 TCS Confidential
Predefined Streams in Java
Used to read-write characters
from-to the console
read() - the basic method
public abstract int read()
throws IOException
Session 3 B: I/O System Java Programming
14 TCS Confidential
Reading Console Input
System.in and
BufferedReader
BufferedReaders int read( )
method reads a character
read() method performs
blocking read - waits for
data if there is none available
Session 3 B: I/O System Java Programming
15 TCS Confidential
Reading Console Input
A single byte is read from the
input stream, converted into
an integer (0 to 255)
-1 is returned on the end of
stream
readLine() method reads a
line
Session 3 B: I/O System Java Programming
16 TCS Confidential
Demo: Reading Console
It reads and displays
characters typed by user
When q is typed, the
program exits
Session 3 B: I/O System Java Programming
17 TCS Confidential
Reading Console Input
System.in is line buffered
No input is actually passed to
the program until ENTER is
pressed.
This makes readLine()
valuable than read() for
interactive console input
Session 3 B: I/O System Java Programming
18 TCS Confidential
Demo: Read Line
Program reads a line from the
console and displays that line
When stop is entered then
program exits
Session 3 B: I/O System Java Programming
19 TCS Confidential
Writing Console Output
Use print() and println()
methods of class PrintStream.
write() method of PrintStream
class a character-based
alternative to print on console
void write(int byteval)
throws IOException
Session 3 B: I/O System Java Programming
20 TCS Confidential
Writing Console Output
public class WriteDemo {
public static void main
(String args[]){
int b; b = A;
System.out.write(b);
System.out.write(\n);
}
}
Session 3 B: I/O System Java Programming
21 TCS Confidential
Writing Console Output ...
print() and println() of
System.out are recommended
for debugging purposes or for
sample programs
Session 3 B: I/O System Java Programming
22 TCS Confidential
Reading & Writing Files
FileInputStreamand
FileOutputStreamclasses
Create byte streams linked to
files
FileInputStream(String fileName)
throws FileNotFoundException
FileOutputStream(String fileName)
throws FileNotFoundException
Session 3 B: I/O System Java Programming
23 TCS Confidential
Reading & Writing Files
read()/write() methods
Remember File should be
closed after its use
Use close() method
void close()
throws IOException
Session 3 B: I/O System Java Programming
24 TCS Confidential
Demo: Count Bytes
The program counts the total
number of bytes coming from
a file, or console if no file is
specified as a command line
argument
Session 3 B: I/O System Java Programming
25 TCS Confidential
Demo: Copy File
The program copies a file to
another file
Two command line
arguments are required
Session 3 B: I/O System Java Programming
26 TCS Confidential
The File Class
Is a subclass of Object that
encapsulates a disk file
It neither operates on streams
nor specifies how information
is retrieved from or stored in
files
Session 3 B: I/O System Java Programming
27 TCS Confidential
The File Class
A file object is used to
obtain/ manipulate file
permissions, time, date, and
directory path
navigate subdirectory
hierarchies
Session 3 B: I/O System Java Programming
28 TCS Confidential
The File Class
Constructors to create File
objects:
File(String dirPath)
File(String dirPath, String fname)
File(File dirObj, String fname)
Session 3 B: I/O System Java Programming
29 TCS Confidential
Demo: File Properties
Program displays properties of a
file supplied as first command
line argument
It copies to another file supplied
as second command line
argument
Deletes a file supplied as third
command line argument
Session 3 B: I/O System Java Programming
30 TCS Confidential
Demo: FileIO
Program expects two
command line arguments
Copies first file contents to
second file
Session 3 B: I/O System Java Programming
31 TCS Confidential
Filtered I/O Streams
FilterInputStreamand
FilterOutputStreamare
streams that modify the
information sent through an
existing stream
Session 3 B: I/O System Java Programming
32 TCS Confidential
Filtered I/O Streams ...
BufferedInputStreamand
BufferedOutputStreamallow
attaching a memory buffer to
the I/O streams
Session 3 B: I/O System Java Programming
33 TCS Confidential
Filtered I/O Streams ...
The PrintStreamclass
provides all of the formatting
capabilities we have been
using from System.out
Session 3 B: I/O System Java Programming
34 TCS Confidential
ByteArray I/O Streams
ByteArrayInputStream,
ByteArrayOutputStreamare
subclasses of InputStream
and OutputStreamthat
support I/O from/to byte array
ByteArrayInputStream(byte array [])
Session 3 B: I/O System Java Programming
35 TCS Confidential
SequenceInputStream
Builds one stream from
multiple input streams
Useful when reading from
multiple data files is required
SequenceInputStream(InputStream
is1, InputStreamis2)
Session 3 B: I/O System Java Programming
36 TCS Confidential
Demo: SequenceInputStream
Program expects two
command line arguments
Reads both file contents and
prints on the console
Session 3 B: I/O System Java Programming
37 TCS Confidential
Summary
The Java I/O System
Stream Types
The Byte Stream Hierarchy
Predefined Streams in Java
The File Class
Byte Stream Hierarchy Classes