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
.classfiles
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.txtNeeds no extra info.
π’ Relative Path
Depends on current directory.
Example:
SampleFile.txtAssumes 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:ssFrom 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?
String converted to byte array
OutputStream points to System.out
write() sends bytes to screen
flush() forces output
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