File Input and Output
File Input and Output
CONTINUING WITH
EXCEPTIONS
... }
CHECKED EXCEPTIONS
In Java, Checked Exceptions are exceptions that are checked at compile
time by the compiler. They represent conditions that a reasonable
application might want to catch, and the Java language ensures that they
are either handled by the code or declared to be handled by any method
that can potentially throw them.
Usually throws and exception if the compiler can’t find the necessary
packages and classes in the program.
{ .. Sample Program:
Output
.. }
{ .. Revised Code:
Output
.. }
Try – Catch Blocks
}
Understanding Computer Files
STORAGE
-Volatile -Nonvolatile
Storage Storage
C. Data
Files D. Program Files /
Application Files
FILES
Directories and Paths
A. Root Directory
B. Folders or Directories
C. Path
Using the Paths and Files Classes
• java.nio.file package
...
import to use the Path and
Files classes • Path class
represents the location of
a file or directory in your
• File Class
computer.
provides various methods
that allow you to perform
operations on files and
directories
Creating a Path
1. Determine the Default 1. Define a Path Using
{ File System getPath()
•You can find out what file You use the getPath()
system your computer is method to create this
using by calling "address" based on your file
FileSystems.getDefault(). system.
}
Creating a Path
3. Absolute Path vs Relative Path
{
A. Absolute Path B. Relative Path
This is the complete This is a shorter, simpler
}
address of a file, path that gives directions
starting from where you are
starting from the very right now.
beginning of your
computer's filing
system.
Retrieving Information About a Path
a. toString() b. getFileName()
- returns the String - returns the file or directory
representation of the Path, denoted by this Path; this is the
eliminating double last item in the sequence of name
backslashes elements
c. getNameCount() d. getName(int)
- returns the number of - returns the name in the
elements in the Path position of the path
specified by the integer
parameter
Sample Code:
Code:
Output:
Converting a Relative Path to an Absolute One
Code:
Output:
Checking File Accessibility
Code:
Output:
Deleting a Path
Code:
Output:
If file still exists in the folder: If file there is no file that matches the path stored
in filePath:
Record
{ Character
*
The smallest unit of A collection of fields that
data represent one complete
set of information.
Field
Files }
Types of File Access
*
Sequential Access File: This type of file stores
records one after another, in order. For example, if
you’re saving a list of students by their ID numbers,
the data is stored in order from the smallest ID to
the largest. To find a specific record, you must start
from the beginning and read through until you reach
it.
Opening and Closing Files
Open a File: When you open a file in Close the File: When you’re done,
a program, you create an object that you close the file. This makes sure
allows you to work with it, and you that the file is saved properly and
connect a stream of bytes (data) to isn’t taking up unnecessary
the file. resources. It's always a good idea to
close any file you open!
} ..
Streams
Think of a stream as a flow of water. In computing, a stream is a flow of data
(bytes) that goes into or out of your program.
Input Stream
...
Data flows into your
program (e.g., reading a Output Stream
file).
Data flows out of your One-Way Traffic
program (e.g., saving
data to a file). Most streams work in one
direction, like one-way roads—
you either read data from them
or write data to them, but not
both at the same time.
Copying the contents of a file into another file.
Code:
Copying the contents of a file into another file.
Files in the directory and its contents:
1. Preserves 2. Simplicity
Data Order
They are simple to read and write,
maintain the order of data, which
which makes them easy to
makes them useful for
implement and manage
applications where data needs to
be processed in the order in which
it was stored
3. Efficiency 4. Persistence
Efficiency: They work well for Sequential files allow data to be
processing large amounts of stored persistently, enabling
data that are accessed in order, programs to retrieve and store
information between different
execution times.
Code:
Output:
Random Access File
A Random Access File allows you to read from and write to any part of a
file at any time, as opposed to sequential access files where data is read
or written in a specific order from beginning to end. This means you can
"jump" to different parts of the file, which makes it useful for situations
where you need to frequently read, update, or modify data without
reading the entire file.
In Java, the RandomAccessFile class provides this functionality, allowing
random access to read and write data.
Random Access File
Features of Random Access File
•Reading and Writing: You can both read from and write to
the same file.
•Direct Access: You can move to a specific location (byte
position) within the file using a file pointer.
•Flexibility: You can overwrite data, append data, or read
data from any part of the file.