Sei sulla pagina 1di 16
UNIT3 {59 8sscinpuouput stream Summary of Unit 3 @ Summary ‘This unit discusses how a program written jn Java performs input! ouput operations. Asa file is a common storage medium used for data storage, the examples and discussions are mostly file related. A file is identified by its file name and path name. To manipulate a file, such as inspecting its acri (or simply Fite) object by providing a st=ing object specifying the full path of the file, including the path name and file name. Furthermore, a Fite object in Java can associate not only with a file bur also with a directory. Then, you can call methods of the File object to obrain the information about the fle. ies, ou can createa java ic.Fate “The Java standard soliwate library provides classes for performing stream-based input/output operations. Stream classes are categorised according to the type of data they process and the direction of data ow. They are surnmarised in the following table, Input | Inpurstream (e-g., Reader stream | ci lernputstrean) (e.g-) FileReader) output | Outputstzean frites stream | (e.g., Fileoutpurstrean) | (e-g-, FileWtrizer) ‘Once you are familiar with the streamn-based input/output approach, you may find such an approach flexible, as you can replace or insert 4 stream object for vacious behaviours. You can then further study the uses of other clases provided by the package that are specific to other storage media of communication channels. Daring the discussion of performing inpuvoutput operations with Java, we discussed input/output operations that may fail due to an ‘exceptional condition. When the statement that performs the ‘operation fails itis known as a tuntime error or exception. In Java, aan exception is an object that encapsulates the information of the runtime error that occurred, UNITS astcinputloutputstream Unit Practice Exercises 1, Attempe the Exercises questions in Malik, D S, Janu Prognomming: From Problem Analysis to Program Design for question 7,9, 10 11, 12 in page 161 to 162. 2. Whae does it mean to say that a program is robust? 3. Why do programming languages require that variables be declared before they ate used? What docs this have to do with correctness and robustness? 4, Consider subroutine processData that has the header static void processData() throws 10Exception Write a cry. ..catch statement that calls this subroutine and prints an error message if an 1oException occurs 5. Why should a subroucine throw an exception when it encounters an error? ‘Why not just terminate the program? 6. Java has ewo types of streams: character streams and byte streams. Why? What is che difference between the two types of streams? 7. What is the point of the following statement? out = new Printlirster( new Filewriter (™data-dac") }7 Why would you need a statement that involves two different stream classes, Printliriter and FiloWriter? a