FILE, INPUT, AND OUTPUT

πŸ’Ύ FILE, INPUT, AND OUTPUT

(IT2408 – 04 Handout 1 Study Guide)


πŸ—‚ 1. Computer Files

πŸ“Œ What is a Computer File?

A computer file is a collection of data stored on a permanent storage device like:

  • Hard disk

  • USB

  • CD

  • Magnetic tape

Files have:

  • πŸ“ Size

  • 🏷 Name

  • πŸ•’ Creation date

In short:

A file is stored data that lives on your storage device.


πŸ“‚ Types of Files

According to page 1, files are categorized by how they store data:

πŸ“ Text Files

  • Encoded in ASCII or Unicode

  • Can be opened in a text editor

  • Examples:

    • Payroll data

    • Program files

    • Application files

You can actually read these.


πŸ’» Binary Files

  • Stored in 0s and 1s

  • Not readable in a text editor

  • Examples:

    • Images

    • Music

    • Compiled .class files

You see weird symbols if you open it in Notepad 😭


πŸ“ Directory Structure & Paths

🏠 Root Directory

Main folder of storage device.

Example:
C:\


πŸ“‚ Folder Hierarchy

Folders inside folders = hierarchy.

Example from page 1:

C:\Java\Chapter.11\Data.txt


πŸ›£ What is a Path?

A path is the complete location of a file in a directory structure.

Windows

Uses backslash:

\

UNIX/Solaris

Uses forward slash:

/

πŸ“Œ 2. Common File Tasks (Very Exam Important)

From page 2:

Applications usually:

  • Check if file exists

  • Open file

  • Write to file

  • Read from file

  • Close file

  • Delete file

Think of it like file life cycle.


🧠 3. Path and Files Classes (Java NIO)

To use:

import java.nio.file.*;

nio = new input/output


πŸ”Ή Path Class

Used to:

  • Store file information

  • Location

  • Size

  • Creation date

  • Check existence

Path creates the reference.


πŸ”Ή Files Class

Used to:

  • Delete files

  • Check attributes

  • Create streams

Files performs the action.

Mnemonic:

Path = Location

Files = Action


πŸ— Creating a Path

Method 1: Using FileSystem

FileSystem fs = FileSystems.getDefault();
Path filePath = fs.getPath("C:\\Java\\Chapter.11\\Data.txt");

⚠ Important:

In Java, \ is an escape character.

So you must write:

\\

Method 2: Using Paths Class (Simpler)

Path filePath = Paths.get("C:\\Java\\Chapter.11\\SampleFile.txt");

This is easier and more common.


πŸ“ Absolute vs Relative Path

From page 3:

πŸ”΅ Absolute Path

Complete path from root.

Example:

C:\Java\Chapter.11\SampleFile.txt

Needs no extra info.


🟒 Relative Path

Depends on current directory.

Example:

SampleFile.txt

Assumes file is in same folder as program.


πŸ”„ Converting Relative to Absolute

Method:

toAbsolutePath()

Example from PathDemo2 program:

User enters filename

  • Program converts it to full path

If already absolute β†’ no change
If relative β†’ converts to absolute


πŸ” Retrieving Path Information

From page 3–4 table:

Method

Purpose

toString()

Returns path as String

getFileName()

Returns last element (file name)

getNameCount()

Returns number of elements

getName(int)

Returns element at index

Important:

  • Index starts at 0

  • Last index = getNameCount() - 1


πŸ” Checking File Accessibility

Method:

checkAccess()

Import:

import static java.nio.file.AccessMode.*;

Options:

  • No argument β†’ check existence

  • READ β†’ check read permission

  • WRITE β†’ check write permission

  • EXECUTE β†’ check execute permission

Alternative:

Files.exists(path)

⚠ Throws IOException if inaccessible.

From PathDemo3 example:

Checks READ and EXECUTE

  • Uses try-catch


πŸ“Š Determining File Attributes

Method:

Files.readAttributes()

Returns:

BasicFileAttributes

Example:

BasicFileAttributes attr =
Files.readAttributes(filePath, BasicFileAttributes.class);

Then you can call:

  • size()

  • creationTime()

  • lastModifiedTime()

FileTime format:

yyyy-mm-ddThh:mm:ss

From page 6

πŸ”„ IO Classes Hierarchy

From diagram on page 6:

Object
β†’ InputStream
β†’ OutputStream
β†’ Reader

Important subclasses:

Input Side

  • FileInputStream

  • BufferedInputStream

Output Side

  • FileOutputStream

  • BufferedOutputStream

  • PrintStream (System.out)

Character Streams

  • Reader

  • BufferedReader

  • BufferedWriter


πŸ“€ OutputStream Methods (Exam Alert 🚨)

From page 7 table:

Method

Purpose

close()

Closes stream

flush()

Forces buffered output

write(byte[])

Writes all bytes

write(byte[], off, len)

Writes portion


πŸ’» ScreenOut Program Explained

From page 7–8:

String s = "ABCDF";
byte[] data = s.getBytes();
OutputStream output = System.out;
output.write(data);
output.flush();
output.close();

What happens?

  1. String converted to byte array

  2. OutputStream points to System.out

  3. write() sends bytes to screen

  4. flush() forces output

  5. close() releases stream

Result:

ABCDF

🎯 QUICK SUMMARY SECTION

🧠 File Types

Text = readable

Binary = unreadable


🧠 Path Types

Absolute = full address

Relative = depends on location


🧠 Path vs Files

Path = describes file

Files = performs operation


🧠 Key Methods to Memorize

Path:

  • toString()

  • getFileName()

  • getNameCount()

  • getName()

Files:

  • exists()

  • readAttributes()

OutputStream:

  • write()

  • flush()

  • close()


🧩 MEMORY TRICKS

πŸ“ File Tasks = OWRRCD

Open

Write

Read

Remove

Check

Delete


πŸ“ Path Types

A = Absolute = All info

R = Relative = Relies on location


πŸ”„ OutputStream Flow

Write β†’ Flush β†’ Close

Mnemonic:

WFC = Write First, Close