GENERICS AND FILE HANDLING Saumya Srivastava (977934) Divyangana Pandey (977790) Shubhi Saxena (978108) Arka Das (962969) AHD05/15-16 AJA 21
February 3, 2011 CONTENTS - * - 111FC GENERICS-INTRODUCTION A Class Definition with a Type Parameter A Generic Constructor Name Has No Type Parameter INTRODUCTION TO FILE HANDLING Java Streams Classes For File Operations Limitations of generics
February 3, 2011 GENERICS-INTRODUCTION New feature in JDK1.5. Generics allow us to abstract over types. Generics make the code clearer and safer. Reduces the runtime cast exceptions. Increase readability and robustness.
February 3, 2011 A Class Definition with a Type Parameter The T in the angle Bracket is the formal parameter. This T gets replaced with the respective actual parameter in the invocation of the method. The generic type declaration complied only once and turned into single class file. Note on naming convention :Recommended to use single characters and avoid lower case.
February 3, 2011 A Generic Constructor Name Has No Type Parameter!!! Although the class name in a parameterized class definition has a type parameter attached, the type parameter is not used in the heading of the constructor definition: public Pair () A constructor can use the type parameter as the type for a parameter of the constructor, but in this case, the angular brackets are not used: public Pair(T first, T second) However, when a generic class is instantiated, the angular brackets are used: Pair pair = new Pair ("Happy", "Day");
February 3, 2011 Limitations of Generics: Arrays such as the following are illegal: Pair [] a = new Pair [10]; Although this is a reasonable thing to do, it is not allowed given the way that Java implements generic classes. Other limitations are as follows: Cannot Create, Catch or Throw objects of parametrized types Cannot instantiate generic types with primitive types. Cannot declare static fields whose types are type parameters. Cannot overload a method where the formal parameter types of each overload erase to the same raw type.
February 3, 2011 Types of files: Text files contain strings of printable characters; they look intelligible to humans when opened in a text editor. Binary files contain numbers or data in non-printable codes; they look unintelligible to humans when opened in a text editor. Java can process both binary and text files, but binary files are more common while doing file I/O. INTRODUCTION TO FILE HANDLING
February 3, 2011 JAVA Streams: Definition: an object that either delivers data to its destination (screen, file, etc.) or takes data from a source (keyboard, file, etc.). It acts as a buffer between the data source and destination. Types: Input stream: a stream that provides input to a program. Eg: System.in Output stream: a stream that accepts output from a program. Eg: System.out
February 3, 2011 Classes For File Operations Output: PrintWriter FileOutputStream or FileWriter Input: BufferedReader FileInputStream and FileReader InputStream and OutputStream are designed for byte streams. Reader and Writer are designed for character streams. The byte stream classes and the character stream classes form separate hierarchies. In general, you should use the character stream classes when working with characters or strings, and use the byte stream classes when working with bytes or other binary objects.
February 3, 2011 To use these we need to import java.io.*; Opening an output file creates a new file if it does not already exist Opening an output file that already exists eliminates the old file and creates a new, empty one. Data in the original file is lost To add/append to a file instead of replacing it, add ‘true’ as second parameter to open file in append mode. EXCEPTIONS FileNotFoundException : Input file not found IOException EOFException: Attempt to read a character after EOF has been reached. To avoid this Always check for the end of the file when reading from a file.
February 3, 2011 CODE FOR FILE INPUT
February 3, 2011 CODE FOR FILE OUTPUT
February 3, 2011 REFERENCES: [1] [2] TCS iON [3] [4] tutorials.jenkov.com [5] Patrick Naughton, Herbert Schildt, “Java 2: The Complete Reference”, Osborne/McGraw-Hill Publication, [6]
Thank you AHD05/15-16 AJA 21