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

File Input and Output

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

File Input and Output

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

{

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

{Standard try-catch Try-with-Resources Block

}
Understanding Computer Files
STORAGE
-Volatile -Nonvolatile
Storage Storage

-Computer -Permanent storage


File devices
FILES
Categories of Files by the Way They Store Data

A. Text Files B. Binary Files

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:

If the directory is not empty:


Determining File Attributes
Code:
Output:
File Organization, Streams, and
Buffers
When you work with data on a computer, sometimes you want to save it
for a long time, so you store it on a permanent storage device, like a hard
drive or USB stick. Here's how this works:
How Data is Stored in Businesses
Businesses often store data in an organized way, using a hierarchy

Record
{ Character

*
The smallest unit of A collection of fields that
data represent one complete
set of information.

A group of characters A bunch of records


that represent a single stored together
piece 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:

Files in the directory and its contents:


Buffers
A buffer is a temporary memory location that holds data while it's being
transferred between two places, such as between your program and a
file. Buffers help optimize performance by reducing the number of direct
read/write operations to slower devices.
Code:
Flushing
Flushing refers to the process of clearing any bytes that have been sent
to a buffer but have not yet been transferred to the output device. This
ensures that all buffered data is properly written out, which is crucial for
data integrity.
Sequential Data Files
Sequential data files are files where data is stored in a linear, ordered
manner. Each piece of data is written and read in the exact order in
which it appears in the file, similar to a list of items that are processed
one after the other. These files are ideal when you want to access data in
a specific sequence.
Sequential Data Files
Importance (Why and Where We Use It?)

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.

You might also like