Cards (19)

  • Many computer applications use some form of persistent data. This is data that is stored in secondary storage and will be available whenever the application is used.
  •  All persistent data is stored in files. This can be a single file, or for larger systems a collection of related files. Some applications use databases.
  • File management is the responsibility of the computer operating system.
  • Most operating systems require that a file is opened before it can be read or modified. When you open a file, you state the name of the file (including its path if it is not in the same folder as your program code) and the mode in which you want to open the file. 
  • It is important to carefully consider which mode to open any file in, as using the wrong mode can result in loss or corruption of data. Read mode is usually the default if you do not explicitly specify a mode when you open a file.
  • When you attempt to open a file, the operating system will check that you (or your application) has the necessary permissions to use the file.
  • Once you have finished working with a file, it must be closed. You should always close a file as soon as it is finished with, so that it can be accessed by other users. If you do not close the file properly, you may find that the operations you have carried out (on the file) are not saved.
  • Read mode: you can read from the file but not modify its contents. By default, read operations will start from the beginning of the file.
  • Write mode: you can write to the file but not read from it. The writing position will default to the start of the file so any existing data will be overwritten. If the file you try to open does not exist, a new file will be created.
  • Append mode: you can write to the file but not read from it. Data that you write will be appended to the end of the file
  • Read/write mode: you can read from and write to the file.
  • When you are working with files, there are lots of things that can go wrong. Because your file exists outside of your program, it is possible for someone or another program to delete it or move it or to open it in a mode that prevents your program from accessing it as needed.
    You must make sure that you anticipate and handle all possible exceptions.
  • text file is made up of one or more lines of plain text. Non-text files are called binary files. These are files which represent more complex data such as images or computer games.
  • Text files can be created, read and amended by computer programs. They can also be opened, viewed and edited in a text editor. There are many different text editors, some come with your operating systems and others can be downloaded.
  • Sometimes you will want to use a text file to store structured data. CSV stands for 'comma-separated values' and is a type of text file where each line contains a set of data values separated by commas. The lines of a CSV file can be referred to as records and the data values as fields.
  • Your text file is encoded as a sequence of characters. The default character coding for text files is usually UTF-8, which is a form of Unicode and is the most common format across the world.
  • Binary files are intended to only be read by a computer system. A program is needed to interpret the data contained in a binary file.
  • When you write a computer program, data is held in variables and in more complex data structures such as arrays, dictionaries, or lists. A binary file allows you to store this data in a form that preserves the structures used in your program
  • Serialisation is the process of converting an object (such as a dictionary of data) into binary sequences that can be stored in a file. When the file is accessed, the binary data is retrieved from the file and deserialised into objects that are exact copies of the original information.