Software Engineering Module Core of Java Topic IO


























- Slides: 26

Software Engineering Module: Core of Java Topic: I/O Stream TALENTSPRINT | © Copyright 2012

I/O Stream By the end of this session, you will be able to: • Differentiate Byte Stream and Character Stream • Describe hierarchy of Input and Output Streams • Read or Write data to/from a resource (file) TALENTSPRINT | © Copyright 2012 2

I/O Stream What is a Stream? A stream is data that you access in sequence. Destination Source TALENTSPRINT | © Copyright 2012 3

I/O Stream What is an I/O Stream? An I/O Stream represents an input source or an output destination. Represent many different kinds of sources and destinations like disk files, devices, other programs, a network socket, and memory arrays. Support many different kinds of data like simple bytes, primitive data types, localized characters, and objects. Some streams simply pass on data; others manipulate and transform the data in useful ways. Note All the I/O classes are present in a package called “java. io”. TALENTSPRINT | © Copyright 2012 4

I/O Stream Input Stream A program uses an input stream to read data from a source, one item at a time. Stream Data Source TALENTSPRINT | © Copyright 2012 01010101 Program 5

I/O Stream Output Stream A program uses an output stream to write data to a destination, one item at time. Stream Program 0101 TALENTSPRINT | © Copyright 2012 01010101 Data Destination 6

I/O Stream Types of stream Byte stream Character stream Input (source) stream Output (destination) stream TALENTSPRINT | © Copyright 2012 7

I/O Stream Types of Stream Byte stream Can access the file byte by byte. Base class for byte stream are: • The Input. Stream Class • The Output. Stream Class Both classes are abstract. TALENTSPRINT | © Copyright 2012 8

I/O Stream Types of Stream Character stream Can access the file character by character. Base class for character stream are: • The Reader Class • The Writer Class Both classes are abstract. TALENTSPRINT | © Copyright 2012 9

I/O Stream Types of Stream Input (source) stream Can read from these streams. Base class for all input stream are: • The Input. Stream Class • The Output. Stream Class TALENTSPRINT | © Copyright 2012 10

I/O Stream Types of Stream Output (destination) stream Can write to these streams. Base class for all output stream are: • The Output. Stream Class • The Writer Class TALENTSPRINT | © Copyright 2012 11

I/O Stream Class Hierarchy File. Input. Stream Line. Number. Input. Stream Piped. Input. Stream Data. Input. Stream Filter. Input. Stream Buffered. Input. Stream Byte. Array. Input. Stream Pushback. Input. Stream Sequence. Input. Stream String. Buffer. Input. Stream Object. Input. Stream TALENTSPRINT | © Copyright 2012 12

I/O Stream Class Hierarchy File. Output. Stream Piped. Output. Stream Data. Output. Stream Filter. Output. Stream Buffered. Output. Stream Byte. Array. Output. Stream Print. Stream Output. Stream Object. Output. Stream TALENTSPRINT | © Copyright 2012 13

I/O Stream Class Hierarchy Buffered. Reader Line. Number. Reader Char. Array. Reader Input. Stream. Reader File. Reader Filter. Reader Pushback. Reader Piped. Reader String. Reader TALENTSPRINT | © Copyright 2012 14

I/O Stream Class Hierarchy Buffered. Writer Char. Array. Writer Output. Stream. Reader Writer File. Writer Filter. Writer Piped. Writer String. Writer Filter. Writer TALENTSPRINT | © Copyright 2012 15

I/O Stream File Class The File class in the Java IO API gives you access to the underlying file system. Using the File class you can only do the following operation on files: Ø Ø Ø Check if a file exists. Read the length of a file. Rename or move a file. Delete a file. Check if path is file or directory. Read list of files in a directory. Note The File only gives you access to the file and its meta data. If you want to read or write the content of files, you can do so using either File. Input. Stream or File. Output. Stream classes. TALENTSPRINT | © Copyright 2012 16

I/O Stream Constructors to create a File object public File(String path) Here path is simply a String which is either full or relative pathname to the file starting from the current working directory. Example: File f 1 = new File("talentsprint. html"); File f 2 = new File("/etc/talentsprint"); TALENTSPRINT | © Copyright 2012 17

I/O Stream Constructors to create a File object public File(String path, String name) Here name is the filename and path is the name of the directory that contains the file. Example: File f 2 = new File("/etc", "talentsprint"); public File(File f, String name) Here 'f' is the file object and name is the filename Example: File f 3=new File(filobj, “talentsprint”); TALENTSPRINT | © Copyright 2012 18

I/O Stream Methods of File Class TALENTSPRINT | © Copyright 2012 19

I/O Stream Program on File Class import java. io. *; public class File. Exists{ public static void main(String args[]){ File file=new File("/home/talentsprint. txt"); boolean exists = file. exists(); if (exists) { System. out. println("File or Directory exist. "); } else{ System. out. println("File or Directory not exists. "); } } } TALENTSPRINT | © Copyright 2012 20

I/O Stream File. Input. Stream Class: File. Input. Stream Class is used for reading data from the files. File. Input. Stream Class File. Input. Stream fis=new File. Input. Stream(String filepath); Example: File. Input. Stream f = new File. Input. Stream("home/talentsprint. txt"); This takes a file name as a string to create inputstream object. File. Input. Stream fis=new File. Input. Stream(File file. Obj); Example: File. Input. Stream f = new File. Input. Stream(f); This takes the file object 'f' to create input stream object. TALENTSPRINT | © Copyright 2012 21

I/O Stream Methods of File. Input. Stream Class TALENTSPRINT | © Copyright 2012 22

I/O Stream File. Output. Stream Class: File. Output. Stream is used to create a file and write data into it. The stream would create a file, if it doesn't already exist, before opening it for output. File. Output. Stream fos=File. Output. Stream(String file. Path) Example: File. Output. Stream f = new File. Output. Stream("home/talentsprint. txt"); This takes a file name as a string to create outputstream object. File. Output. Stream fos=File. Output. Stream(File file. Obj) Example: File. Output. Stream f = new File. Output. Stream(f); This takes the file object 'f' to create output stream object. TALENTSPRINT | © Copyright 2012 23

I/O Stream Methods of File. Output. Stream Class TALENTSPRINT | © Copyright 2012 24

I/O Stream 1 2 /* This program reads the contents of file 'talent. txt' and writes the contents 3 on to another file named 'a. txt'. */ 4 5 import java. io. *; 6 class File. Read. Write. Demo{ 7 public static void main(String args[]){ 8 try{ 9 File f=new File("talent. txt"); 10 File. Input. Stream fis=new File. Input. Stream(f); 11 File. Output. Stream fos=new File. Output. Stream(“a. txt”); 12 int i=fis. read(); 13 14 while(i!=-1){ 15 fos. write(i); 16 i=fis. read(); 17 } 18 } 19 catch(Exception e){ 20 System. out. println("file not found"); 21 } 22 } 23 } Note: The program will create file 'a. txt', if the file is not existing. TALENTSPRINT | © Copyright 2012 25

I/O Stream TALENTSPRINT | © Copyright 2012 26