File Handling (CIE IGCSE Computer Science)

Revision Note

Test Yourself
Becci Peters

Expertise

Computer Science

Why Use Files?

  • Files provide a way to ​store data ​permanently, allowing programs to access and manipulate it later
  • Files allow for ​organised storage​ of data, making it easier to find, access, and update
  • They enable ​sharing​ of data between different programs and users
  • They support ​data backup, ensuring data is not lost when a program or computer system shuts down
  • They allow for ​large amounts of data​ to be stored and managed efficiently
  • Data can be transported from one system to another

Opening & Closing Files

  • Common file operations include:
    • Opening: creates a connection between the file and the program
    • Reading: retrieves data from a file
    • Writing​: saves new data to a file, overwriting existing content
    • Appending: adds new data to the end of a file without overwriting existing content
    • Closing: ends the connection between the file and the program, releasing system resources
  • Single items of data can be numbers, characters, or other simple data types
  • A line of text typically ends with a newline character or an end-of-line marker

Pseudocode example:

OPEN "file.txt" 
filedata =  READ data
PRINT filedata
CLOSE file

OPEN "file.txt"
WRITE data
CLOSE file

Python example:

with open("file.txt", "r") as file:
    data = file.read()
    print(data)

with open("file.txt", "w") as file:
    file.write(data)

Java example:

    public static void main(String[] args) {
        String path = ("file.txt");

        try {

      FileReader f = new FileReader(path);

     BufferedReader reader = new BufferedReader(f);
            String data = reader.readLine();

      reader.close();

      BufferedWriter out = new BufferedWriter(f);

      out.write(data);

      out.close();

   } catch (Exception e) {

      System.err.println("No file found");

   }

}

Visual Basic example:

Dim File As New System.IO.StreamReader("file.txt")

Console.WriteLine(File.ReadLine())

File.Close()

Dim fileWrite As New System.IO.StreamWriter("file.txt")

fileWrite.WriteLine(data)

fileWrite.Close()

Exam Tip

  • Remember to close files after using them

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Becci Peters

Author: Becci Peters

Becci has been a passionate Computing teacher for over 9 years, teaching Computing across the UK helping to engage, interest and develop confidence in the subject at all levels. Working as a Head of Department and then as an educational consultant, Becci has advised schools in England, where her role was to support and coach teachers to improve Computing teaching for all. Becci is also a senior examiner for multiple exam boards covering GCSE & A-level. She has worked as a lecturer at a university, lecturing trainee teachers for Computing.