Java File IO 1
Java File IO 1
I/O
Continuing with
Exceptions
Checked
Exceptions
Output
:
Try-Catch
Standard try-catch Blocks Try-with-Resource
block • Blockresources
Automatically manages
• Used to handle exceptions in code
(e.g., closes streams) after
blocks.
execution.
• Requires manual resource
• Simplifies code by ensuring
management (e.g., closing
resources are closed, reducing the
streams).
risk of leaks.
Understanding Computer
Files
Volatile Storage Nonvolatile Storage
• Permanent
• Temporary
Permanent Storage
Devices
• Hard disks
• Zip disks
• USB drives
• Reels or
cassettes
• Compact disks
Paths and
Directories
If file already
exists:
Field: A group of characters that represent a single piece of information. For example, your first name would be a
field.
Record: A collection of fields representing one complete set of information. For example, all the complete details
about one customer (name, age, address) make up a record.
Files: A bunch of records stored together. For example, a file might have records of all customers.
Types of File
Access
Sequential Access File
• This type of file stores records one after another, in order.
• 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.
Creating a
File
The same with creating a path, creating a file uses
the File class. The method .createNewFile() will be
used.
Sample Results
Program: If file: is created successfully
Sample Outputs:
Program: If path is deleted
successfully:
Output:
Reading and Writing a Using
File Streams
Appending a
File
Sample Updated file
Program
Output
:
Reading and Writing a Using
File Buffers
Reading and Writing Together
(Copying a file)
Results