MODULE 5 FILE HANDLING AND EXCEPTIONS IN JAVA CHAPTER 1 File Handling and Exceptions
Exception Handling 5. File Handling and Exceptions in Java When executing Java code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things When an error occurs, Java will normally stop and generate an error message. The technical term for this : Java will throw an exception (throw an error)
Exception Handling 5. File Handling and Exceptions in Java Java defines several types of exceptions that relate to its various class libraries. Java also allows users to define their own exceptions.
Exception Handling 5. File Handling and Exceptions in Java Types of built-in Exceptions
Exception Handling 5. File Handling and Exceptions in Java Types of built-in Exceptions
Java try and catch 5. File Handling and Exceptions in Java The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block. The catch keywords come in pairs
Java try and catch 5. File Handling and Exceptions in Java
The finally Keyword 5. File Handling and Exceptions in Java The finally block in a try-catch-finally contains the code for cleaning up after a try or a catch.
The finally Keyword 5. File Handling and Exceptions in Java
The finally Keyword 5. File Handling and Exceptions in Java Code in a finally block is always executed despite the following scenarios: Forced exit occurs using a return, continue or a break statement Normal completion Caught exception thrown - Exception was caught and thrown in a method Uncaught exception thrown - Exception thrown was not specified in any catch block in the method
Coding Simulation 5. File Handling and Exceptions in Java
Reading and Writing Files 5. File Handling and Exceptions in Java Hierarchy of classes dealing with Input and Output streams
FileInput Stream 5. File Handling and Exceptions in Java Used for reading data from the files A constructor taking a file name as string to create an input stream object to read the file: InputStream f = new FileInputStream (“C:/java/hello”); A constructor taking a file object to create an input stream object to read the file; File f = new File(“C:/java/hello”); InputStream f = new FileInputStream (f);
FileOutput Stream 5. File Handling and Exceptions in Java Used to create a file and write data into it. The stream will create a file, if it is not existing before opening it for output. A constructor takes a file name as a string to create an output stream object to write the file: OutputStream f = new FileOutputStream (“C:/java/hello”); A constructor takes a file object to create an output stream object to write the file; File f = new File(“C:/java/hello”); OutputStream f = new FileOutputStream (f);
File Navigation and I/O 5. File Handling and Exceptions in Java File Class Represents the files and directory path name Used to create files and directories, file searching, file deletion, etc.
File Navigation and I/O 5. File Handling and Exceptions in Java File Class
File Navigation and I/O 5. File Handling and Exceptions in Java File Class To create a file (assuming that the current directory already existed).
File Navigation and I/O 5. File Handling and Exceptions in Java File Class To delete a file (assuming that the file already existed).
File Navigation and I/O 5. File Handling and Exceptions in Java FileReader Class Inherits from the InputStreamReader class Used for reading streams of characters
File Navigation and I/O 5. File Handling and Exceptions in Java FileReader Class
File Navigation and I/O 5. File Handling and Exceptions in Java FileWriter Class Inherits from the OutputStreamWriter class Used for writing streams of characters
File Navigation and I/O 5. File Handling and Exceptions in Java FileWriter Class
Directories in Java 5. File Handling and Exceptions in Java A file which can contain a list of other files and directories Use file objects to create directories, to list down files available in a directory File Utility Methods (creating directories) mkdir () method – creates a directory, returns true(success) and false (failure) mkdirs () method – creates both a directory and all the parents of the directory
Directories in Java 5. File Handling and Exceptions in Java Creating a directory - Example
Coding Simulation 5. File Handling and Exceptions in Java
Sources https://www.tutorialspoint.com/java/index.htm https://www.javatpoint.com/java-tutorial https://dev.java/learn/getting-started/ https://www.w3schools.com/java/default.asp 5. File Handling and Exceptions in Java