Module 5_Ch1 - File Handling And Exception.pptx

jhesorleylaid2 25 views 26 slides Sep 06, 2024
Slide 1
Slide 1 of 26
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26

About This Presentation

File Handling and Exception in Java


Slide Content

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