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