Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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