Download presentation
Presentation is loading. Please wait.
Published byClifford Eaton Modified over 8 years ago
1
Generics and file handling Presented by: Abha Kumari Neha Pradip Vhanmane Raj Visa Rashmi Kewlani Suvrat Dixit
2
Content: ● Generics – Introduction – Types & Methods ● File I/O – Introduction – Streams – Files – Directories
3
Introduction ● Introduced in J2SE 5.0 ● Enhancement which allows working on various types at once. ● Add compile time type safety to the collection's framework. ● Only works with object and not with primitive types. ● Nothing but parameterized types. ●
4
Benefits ● Stronger type cast check at the compiler time. ● Elimination of type casting. ● Ensure type safety. ● Reusabilty of resources.
5
Example
8
Type Parameter Naming Convention ● Type parameter names are : – Single. – Uppercase letters. ● Used to simplify the difference between a type variable name and an ordinary class or interface name.
9
Most commonly used type parameter names are: ● K-Key ● N-Number ● T-Type ● V-Value ● S,U,V etc -2 nd,3 rd,4 th types.
10
Invoking And Instantiating A Generic Type ● The Diamond ● Multiple type parameter ● Bounded type parameter ● Parameterized ● Row type
11
The Diamond ● Used from JDK-7 onwards ● No need to specify the type on right hand side. ● It can be left as the diamond type. ● Box integerBox=new Box (); ● can be written as: ● Box integerBox=new Box<>();
12
Multiple type parameter ● More than one type can be used in a class.
13
Multiple Type Parameter: ● Value should be given according to declared type,respectively. ● If not given respectively it will throw a compile time error.
14
Bounded type parameter ● Syntax : class class_name ● Restricts the type of object T with type1 – Bounds T with number datatypes
15
Parameterized type ● ArrayList – Defines the type E by substituting object having parameter – Throws error if casted with different type ● Syntax: – Classname
16
Raw type ● Does not define any datatype during instantiation ● Used as Backward compatibility – To make the code compatible for versions below 5.0
17
Generic Methods ● Rules to define Generic Methods: – Contains one or more type parameters,separated by commas – One or more type parameters – Declared like that of any other method – Parameters can represent only reference types
18
Files and I/O ● Stream : A sequence of data – InputStream – OutputStream ● Types of Stream Classes – Byte Stream – Character Stream
19
Byte Stream ● Uses 8-bit byte for input and output. ● Can be classified into two abstract classes ● Reader – int read() – int read(byte[]) ● Writer – void write(int b) – void write(byte[])
20
Character Stream ● Uses 16-bit unicode for input and output ● Can be classified into two abstract classes ● Reader – int read() – Int read(char[]) ● Writer – void write (int c) – Void write(char[]) –
21
Standard Streams ● Standard Input – System.in ● Standard Output – System.out ● Standard Error – System.err
22
Java – Files and I/O Java.io InputStream OutputStream Reader Writer File FileInputStream FileOutputStream FileReader FileWriter
23
File ● Class in Java package that represents a physical file on hard disk ● Operations related to operating system commands can be done on file by creating object of this class like read, write, delete ● Has a constructor that takes string parameter i.e. file path – File nfile=new File(“D:\\”) – nfile: reference to the file object in the Java memory
24
File Methods ● boolean isFile() : checks whether the file represented by object is actually file or not ● boolean isDirectory() : returns true if path is of directory ● long length() : returns the length of the file ● boolean createNewFile() : creates a new file in hard disk
25
Reading and Writing Files ● For Byte Stream: – FileInputStream : class extend InputStream for reading bytes from a file – FileOutputStream : class extend OutputStream for writing to a file ● For Character Stream: – FileReader : class extend Reader for reading bytes from a file – FileWriter : class extend Writer for writing to a file
27
Directories in Java ● File that contains a list of other files and directories ● Operations done on directories using File class ● Creating Directories – mkdir()- creates directory, returns true on success and false on failure – mkdirs()- creates both directory and the parents of the directory ● Listing Directories – list() method
30
THANK YOU
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.