0% found this document useful (0 votes)
5 views

Java File IO 1

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java File IO 1

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Java File

I/O
Continuing with
Exceptions
Checked
Exceptions

Checked exceptions in Java are exceptions verified at compile time. They


represent conditions that applications should catch, ensuring they are
either handled in the code or declared by any method that might throw
them.

Usually throws an exception if the compiler can’t find the necessary


packages and classes in the program.
Sample
Program:

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

• Computer memory or random access memory


• Computer storage or Read-only Memory (ROM)
(RAM)

• Permanent
• Temporary

Permanent Storage
Devices
• Hard disks
• Zip disks
• USB drives
• Reels or
cassettes
• Compact disks
Paths and
Directories

Root DIrectories Folders and Directories Paths

This is the "address" of a file,


The main folder or starting point showing you exactly where it’s
of all the files and folders on a These are containers that hold located on your computer. It
computer's storage device (like files or other folders, helping includes the drive name, folders,
the C: drive on a Windows organize your data. and file name.
computer).
Example: (C:\Users\Documents)
Types of
Paths

Absolute Path Relative Path

• This is a shorter, simpler path that gives


• This is the complete address of a file, starting
directions starting from where you are right now.
from the very beginning of your computer's filing
• If you’re already in any folder, you can just say
system.
Data.txt, and the file will be saved in the same
• Example: C:\\Java\\Documents\\Data.txt
folder or path as your program.
Creating a
Path
To create a path, the File class is used, along with its methods such as exists() and
mkdirs().
Sample Outputs
Program: :If path is created
successfully:

If file already
exists:

If something goes wrong (e.g. the directory is


restricted:
Converting Relative Path to
To convert a relativeAbsolute Path
path to an absolute one, use the .getAbsoluteFile()
or .getAbsolutePath() methods to get the entire path from the root directory
Sample Outputs:
Program:
Deleting a
Path
To delete a path, use the .delete()
method
Sample Outputs:
Program: If path is deleted
successfully:

If path deletion is has a problem


(e.g. there is no path to delete, path is not
empty,
or path is restricted):

notes for the method delete()


the method cannot delete non-empty
directories,
restricted directories and paths, and in bulks.
the method can only delete one file at a time.
File
s
Categories of Files by the Way They
Store Data
Program or
Text Files Binary Files Data Files
Application Files

• Files that store data in a


• Files that contain • These files store facts,
format that computers • Files that contains
information readable by figures, and other
understand but humans instructions that tell your
humans structured information.
can’t. computer how to run
software or applications
• Contains letters, • Often contain data that
• Uses 0s and 1s (binary
numbers, and symbols program need to
code) to store info • Examples: Software
that can be seen function.
installation files with
• Examples: photos extensions
• Examples are files with • Examples: files with
(.png .jpeg .heif), videos like .exe(Windows), .app(
extension format of .txt format extension .dat
(.mp4 .mov), music Mac), or .apk(Android).
or .docx or .pptx
(.mp3)
Hierarchy of Storing
Data
Character: The smallest unit of data, like a single letter, number, or symbol.

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.

Random Access File


• allows you to read from and write to any part of a file at any time

• 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

If something goes wrong


If file already
(e.g. directory is restricted)
exists:
Deleting a
File
The same with deleting a path, the method delete() is
used.

Sample Outputs:
Program: If path is deleted
successfully:

If path deletion is has a


problem
(e.g. file is restricted):
Reading and Writing a
File
Using Using
Streams
Think of a stream as a flow of water. In computing, a A buffer is a temporary memory locationBuffers
that holds data
stream is a flow of data (bytes) that goes into or out of while it's being transferred between two places, such as
your program. between your program and a file. Buffers help optimize
performance by reducing the number of direct read/write
• Input Stream: Data flows into your program (e.g., operations to slower devices.
reading a file).
• Output Stream: Data flows out of your program (e.g.,
saving data to a file).
• One-Way Traffic: Most streams work in one direction,
like one-way roads—you either read data from them or
write data to them, but not at the same time.
Reading and Writing a Using
File Streams
Reading a
File
Sample Source file: Output:
Program
Reading and Writing a Using
File Streams
Writing a
File
Sample Program Created file

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

Source file Copied file with other


lines
Reading and Writing a Using
File Buffers
Appending a
File
Sample Updated file
Program
Random Access
Features of Random Access
Files
A Random Access File allows you to read from
and write to any part of a file at any time, as • Reading and Writing: Files
You can both read
opposed to sequential access files where data from and write to the same file.
is read or written in a specific order from • Direct Access: You can move to a specific
beginning to end. This means you can "jump" location (byte position) within the file using
to different parts of the file, which makes it a file pointer.
useful for situations where you need to • Flexibility: You can overwrite data, append
frequently read, update, or modify data data, or read data from any part of the file.
without reading the entire file.

In Java, the RandomAccessFile class provides


this functionality, allowing random access to
read and write data.
Sample
Program
Terminal Output File Content after all
operations
Thank
You
Presented by
Group 1
Members
Emeterio Keith
Gabutan Jerhon Clyde
Mabangkit Anarica
Nepomucen Clint Justine
o Zander Gene
Tambiga Kenneth
Viovicente Reniel
Qui
z

You might also like