An event that occurs during the execution of a program that disrupts the normal flow of instructions
Exception handling
The process used to change the normal flow of code execution if a specified exception occurs
Checked exceptions
ClassNotFoundException
IllegalAccessException
InstantiationException
NoSuchMethodException
Unchecked (runtime) exceptions
ArithmeticException
ArrayIndexOutOfBoundsException
ArrayStoreException
InputMismatchException
NullPointerException
NumberFormatException
StringIndexOutOfBoundsException
try block
A block of code that might throw an exception that can be handled by a matching catch block
catch block
A segment of code that can handle an exception that might be thrown by the try block that precedes it
The getMessage() method can be used to determine Java's message about the exception
Only one (1) try block is accepted in a program but there can be multiple catch blocks
User-defined exception
Created by extending the Exception class
finally block
Contains statements which are executed whether or not an exception is thrown
There can only be one (1) finally block after a try-catch structure but it is not required
A throw statement sends an exception out of a block or a method so it can be handled elsewhere
Volatile storage
Temporary. Volatile values (stored in variables) are lost when the computer loses its power. The Random Access Memory (RAM) is being used when a Java program stores a value in a variable.
Non-volatile storage
Permanent. A Java program that is saved on a disk uses a non-volatile storage.
Computer file
A collection of data stored on a non-volatile device.
Types of files
Text file
Binary file
Text file
Consists of data that can be read in a text editor. Data in a text file is encoded using a scheme. The most common schemes are ASCII and Unicode. Examples are program files and application files.
Binary file
Contains data that is not encoded as text. This file has contents in binary format, which means they cannot be understood by viewing them in a text editor. Examples are images, music, and the .class extension files which are created after compiling Java programs.
The common characteristics of a text file and binary file are size, name, and date and time of creation.
Permanent files
Commonly stored in the main directory or the root directory.
Folders or directories
Used to organize stored files.
Path
The complete list of the disk drive plus the hierarchy of directories in which the file is located.
In the Windows operating system, the backslash (\) is the path delimiter – the special character used to separate path components.
Path class
Creates objects that contain information about files and directories, such as sizes, locations, creation dates, and is used to check whether a file or directory exists.
Files class
Performs operations on files and directories, such as determining their attributes, creating input and output streams, and deleting them.
Absolute path
A complete path; it does not require any other information to locate a file on a system.
Relative path
Depends on other path information.
Path methods
toString()
getFileName()
getNameCount()
getName(int)
To convert a relative path to an absolute path
The toAbsolutePath() method is used.
To verify if a file exists and if a program can access it when needed
The checkAccess() method is used.
Arguments of the checkAccess() method
None
READ
WRITE
EXECUTE
To remove a file
The delete() method of the Files class is used.
Examples of exceptions that can be thrown when deleting a file: NoSuchFileException, DirectoryNotEmptyException, SecurityException, IOException.
To remove a file without encountering an exception if the file does not exist
The deleteIfExists() method can be used.
To retrieve useful information about a file
The readAttributes() method of the Files class is used.
BasicFileAttributes methods
size()
creationTime()
lastModifiedTime()
compareTo()
Character
The smallest useful piece of data. A character can be any letter, number, or other special symbol (such as punctuation mark) that makes up data.
Field
A group of characters that has some meaning.
Record
A collection of fields that contain data about an entity.