Download Input/Output System Java Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Input/Output System
Java Programming
Session 3 – B: I/O System Java Programming
TCS Confidential
1
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
TCS Confidential
2
The Java I/O System
Memory
Hard Disk
Java
Program
Session 3 – B: I/O System Java Programming
TCS Confidential
3
The Java I/O System
ƒ Streams
ƒ Java I/O
ƒ Physical Devices
Java
Program
File
Stream (Java I/O API)
Memory
ƒ Input Stream
ƒ Output Stream
Session 3 – B: I/O System Java Programming
TCS Confidential
4
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
TCS Confidential
5
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
TCS Confidential
6
Character Streams
ƒ Main abstract classes
ƒReader
ƒWriter
ƒ Concrete subclasses
implements required methods
Session 3 – B: I/O System Java Programming
TCS Confidential
7
Stream Types – Java I/O
Abstract Classes
InputStream
Byte Based
OutputStream
Streams
Reader
Character Based
Writer
Session 3 – B: I/O System Java Programming
TCS Confidential
8
The Byte Stream Hierarchy
Object
InputStream
OutputStream
FileInputStream
FilterInputStream
ByteArrayInputStream
SequenceInputStream
PipedInputStream
FileOutputStream
FilterOutputStream
ByteArrayOutputStream
PipedOutputStream
BufferedOutputStream
PrintStream
BufferedInputStream
PushbackInputStream
Session 3 – B: I/O System Java Programming
TCS Confidential
9
Java I/O Methods
Commonly Used Methods
ƒ InputStream
)read(), close()
ƒ OutputStream
)write(), flush(), close()
Session 3 – B: I/O System Java Programming
TCS Confidential
10
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
TCS Confidential
11
Predefined Streams in Java
ƒ java.lang.System class
ƒ System contains 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
TCS Confidential
12
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
TCS Confidential
13
Reading Console Input
ƒ System.in and
BufferedReader
ƒ BufferedReader’s 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
TCS Confidential
14
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
TCS Confidential
15
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
TCS Confidential
16
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
TCS Confidential
17
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
TCS Confidential
18
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
TCS Confidential
19
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
TCS Confidential
20
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
TCS Confidential
21
Reading & Writing Files
ƒ FileInputStream and
FileOutputStream classes
ƒ Create byte streams linked to
files
FileInputStream(String fileName)
throws FileNotFoundException
FileOutputStream(String fileName)
throws FileNotFoundException
Session 3 – B: I/O System Java Programming
TCS Confidential
22
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
TCS Confidential
23
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
TCS Confidential
24
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
TCS Confidential
25
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
TCS Confidential
26
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
TCS Confidential
27
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
TCS Confidential
28
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
TCS Confidential
29
Demo: FileIO
ƒ Program expects two
command line arguments
ƒ Copies first file contents to
second file
Session 3 – B: I/O System Java Programming
TCS Confidential
30
Filtered I/O Streams
ƒ FilterInputStream and
FilterOutputStream are
streams that modify the
information sent through an
existing stream
Session 3 – B: I/O System Java Programming
TCS Confidential
31
Filtered I/O Streams ...
ƒ BufferedInputStream and
BufferedOutputStream allow
attaching a memory buffer to
the I/O streams
Session 3 – B: I/O System Java Programming
TCS Confidential
32
Filtered I/O Streams ...
ƒ The PrintStream class
provides all of the formatting
capabilities we have been
using from System.out
Session 3 – B: I/O System Java Programming
TCS Confidential
33
ByteArray I/O Streams
ƒ ByteArrayInputStream,
ByteArrayOutputStream are
subclasses of InputStream
and OutputStream that
support I/O from/to byte array
ByteArrayInputStream(byte array [])
Session 3 – B: I/O System Java Programming
TCS Confidential
34
SequenceInputStream
ƒ Builds one stream from
multiple input streams
ƒ Useful when reading from
multiple data files is required
SequenceInputStream(InputStream
is1, InputStream is2)
Session 3 – B: I/O System Java Programming
TCS Confidential
35
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
TCS Confidential
36
Summary
ƒ 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
TCS Confidential
37
Related documents