Computer-science_A-level_Cie
-
computers-and-components6 主题
-
logic-gates-and-logic-circuits2 主题
-
central-processing-unit-cpu-architecture6 主题
-
assembly-language-4 主题
-
bit-manipulation1 主题
-
operating-systems3 主题
-
language-translators2 主题
-
data-security3 主题
-
data-integrity1 主题
-
ethics-and-ownership3 主题
-
database-concepts3 主题
-
database-management-systems-dbms-1 主题
-
data-definition-language-ddl-and-data-manipulation-language-dml1 主题
-
computational-thinking-skills1 主题
-
algorithms14 主题
-
data-types-and-records2 主题
-
arrays2 主题
-
files1 主题
-
introduction-to-abstract-data-types-adt1 主题
-
programming-basics1 主题
-
constructs2 主题
-
structured-programming1 主题
-
program-development-life-cycle2 主题
-
program-design-2 主题
-
program-testing-and-maintenance3 主题
-
user-defined-data-types1 主题
-
file-organisation-and-access-3 主题
-
floating-point-numbers-representation-and-manipulation3 主题
-
protocols2 主题
-
circuit-switching-packet-switching1 主题
-
processors-parallel-processing-and-virtual-machines5 主题
-
boolean-algebra-and-logic-circuits4 主题
-
purposes-of-an-operating-system-os3 主题
-
translation-software3 主题
-
encryption-encryption-protocols-and-digital-certificates3 主题
-
artificial-intelligence-ai4 主题
-
recursion1 主题
-
programming-paradigms4 主题
-
object-oriented-programming7 主题
-
file-processing-and-exception-handling2 主题
-
data-representation5 主题
-
multimedia3 主题
-
compression2 主题
-
networks-and-the-internet11 主题
file-handling
File operations
What are file-processing operations?
-
File-processing involves interacting with external files stored on secondary storage (e.g. hard drives, SSDs)
-
In programming, files must be properly opened, read/written, and closed to ensure data is handled correctly
-
Different types of file access are also used
Common file operations
|
Operation |
Description |
|---|---|
|
|
The file is opened in a specific mode: |
|
|
Data (e.g. lines or records) is read from the file into memory |
|
|
Data is written to the file (overwriting or appending depending on mode) |
|
|
Ends access to the file and ensures all data is saved properly |
Types of file access
|
Access type |
Description |
|---|---|
|
Serial access |
Records are stored one after another, and accessed in the order they were added. Often used for logging |
|
Sequential access |
Records are read from the beginning to the end, in order. Common for batch processing and text files |
|
Random access |
Records can be accessed directly using a key or file position. Common in databases or indexed files |
Programming file-operations
Pseudocode
Basic file operations
OPEN filename FOR mode // mode: READ, WRITE, APPEND
READ record FROM filename // Read one record
WRITE record TO filename // Write one record
CLOSE filename // Always close the file
Serial access (e.g. logging events)
-
Data is written one after another (no specific order)
-
Often used with APPEND mode
Example: Writing logs serially
TYPE LogRecord DECLARE message : STRING DECLARE timestamp : STRING
ENDTYPE DECLARE log : LogRecord SET log.message = "System started"
SET log.timestamp = "12:00" OPEN "logfile.txt" FOR APPEND
WRITE log TO "logfile.txt"
CLOSE "logfile.txt"
Sequential access (e.g. reading through all records)
-
Records are read in order from start to end
-
Common for reports or summaries
Example: Reading student grades sequentially
TYPE Student DECLARE name : STRING DECLARE grade : INTEGER
ENDTYPE DECLARE student : Student OPEN "students.txt" FOR READ WHILE NOT EOF("students.txt") READ student FROM "students.txt" OUTPUT student.name, student.grade
ENDWHILE CLOSE "students.txt"
Random access (e.g. accessing specific records by key)
-
Accesses records directly using a key (e.g. record number or unique ID)
-
Requires indexed files or fixed-length records
Example: Update a student’s grade by record position
TYPE Student DECLARE name : STRING DECLARE grade : INTEGER
ENDTYPE DECLARE student : Student
DECLARE recordNumber : INTEGER SET recordNumber = 5 // go directly to the 5th record OPEN "students.txt" FOR RANDOM
SEEK "students.txt", recordNumber
READ student FROM "students.txt" SET student.grade = 90 // update grade SEEK "students.txt", recordNumber
WRITE student TO "students.txt"
CLOSE "students.txt"
Summary
|
Operation |
Purpose |
Example |
|---|---|---|
|
|
Opens a file in a specified mode |
|
|
|
Reads one record |
|
|
|
Writes one record |
|
|
|
Closes the file |
|
|
|
Moves to a specific record (random access) |
|
Python
Basic file operations
file = open("filename.txt", "r" or "w" or "a") # Open for read, write, or append
line = file.readline() # Read one line
file.write(data) # Write one line
file.close() # Close the file
Serial access (e.g. logging events)
Example: Appending log entries to a file (serial write)
from datetime import datetime message = "System started"
timestamp = datetime.now().strftime("%H:%M") log = f"{timestamp} - {message}n" with open("logfile.txt", "a") as file: file.write(log)
Sequential access (e.g. reading through all records)
Example: Reading through each student line by line
with open("students.txt", "r") as file: for line in file: name, grade = line.strip().split(",") print(f"Name: {name}, Grade: {grade}")
Random access (e.g. accessing specific records by key)
Example: Update a student’s grade by index (record number)
record_number = 4 # 5th record (0-indexed) with open("students.txt", "r") as file: records = file.readlines() name, grade = records[record_number].strip().split(",")
records[record_number] = f"{name},90n" # Update grade to 90 with open("students.txt", "w") as file: file.writelines(records)
Summary
|
Operation |
Python Example |
|---|---|
|
Open |
|
|
Read |
|
|
Write |
|
|
Close |
|
|
Random access |
|
Java
Basic file operations
BufferedReader reader = new BufferedReader(new FileReader("filename.txt"));
String line = reader.readLine();
reader.close(); BufferedWriter writer = new BufferedWriter(new FileWriter("filename.txt", true)); // true = append
writer.write("Some text");
writer.newLine();
writer.close();
Serial access (e.g. logging events)
Example: Appending log entries to a file (serial write)
import java.io.*;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter; public class SerialAccessExample { public static void main(String[] args) throws IOException { String message = "System started"; String timestamp = LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm")); String log = timestamp + " - " + message; BufferedWriter writer = new BufferedWriter(new FileWriter("logfile.txt", true)); writer.write(log); writer.newLine(); writer.close(); }
}
Sequential access (e.g. reading through all records)
Example: Reading through each student line by line
import java.io.*; public class SequentialAccessExample { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new FileReader("students.txt")); String line; while ((line = reader.readLine()) != null) { String[] parts = line.split(","); String name = parts[0]; String grade = parts[1]; System.out.println("Name: " + name + ", Grade: " + grade); } reader.close(); }
}
Random access (e.g. accessing specific records by key)
Example: Update a student’s grade by index (record number)
import java.io.*;
import java.util.*; public class RandomAccessExample { public static void main(String[] args) throws IOException { List<String> records = new ArrayList<>(); int recordNumber = 4; // 5th line (0-indexed) BufferedReader reader = new BufferedReader(new FileReader("students.txt")); String line; while ((line = reader.readLine()) != null) { records.add(line); } reader.close(); // Update the grade String[] parts = records.get(recordNumber).split(","); String updatedRecord = parts[0] + ",90"; records.set(recordNumber, updatedRecord); // Overwrite the file BufferedWriter writer = new BufferedWriter(new FileWriter("students.txt")); for (String record : records) { writer.write(record); writer.newLine(); } writer.close(); }
}
Summary
|
Operation |
Java Example |
|---|---|
|
Open (read) |
|
|
Read |
|
|
Write |
|
|
Append |
|
|
Close |
|
|
Random access |
|
Responses