File Handling/Managing Input/Output

Description

Flashcards for the File Handling unit of the AS SSD course by CCEA.
Ruth Hyndman
Flashcards by Ruth Hyndman, updated more than 1 year ago
Ruth Hyndman
Created by Ruth Hyndman over 8 years ago
99
0

Resource summary

Question Answer
What is File Processing and what is it useful for? When you open a file for reading or writing, it becomes a stream which is a sequence of bytes travelling from a source to a destination over a communication path. The two basic streams are input and output streams. Input stream is used to read and output stream is used to write. File Processing allows data to be stored permanently onto backing store which can be read back at a later date as required.
Name the two main formats for file processing and describe them. -Text files are used for character based data such as simple reports, quiz questions. They take the form of a sequence of printable characters, organised line by line; each of which is terminated by an end-of-line marker (return). It can be opened and read in a text editor. When using text files, you read or write a line of text at a time as a string. You open a text file for reading or writing. You can have more than one file open at any one time, such as one for reading and another for writing. If you want individual characters when using C#, you need to use string-handling functions once you have read a whole line from the keyboard. -Binary files contain mostly unprintable characters. Binary files (AKA “A file of records”) usually contains multiple data types. data is written in blocks of data as bytes in pure binary format. This type of output is useful for storing objects in an efficient way —E.g. a file of Student objects that you need to permanently keep on disk, You must know the organisation of the record in a binary file to be able to read it.
^^^^^^ NB: To be able to undertake file handling using StreamReader/Writer or BinaryReader/Writer - using System.IO has to be added to the namespace.
Location of text or binary files The files created below e.g. “test1.txt” are created in the same directory as the program’s exe file. The file name is a parameter to the constructor for the class of the type StreamWriter, which represents a text file which you can write to. You can specify a full path to the file if you want it to be placed somewhere else e.g. “D:\\AS SSD\.........\\streamtext.txt” (saving to your pen drive) There are three main Classes while deal with Streams i.e. FileStream, StreamReader/Writer and BinaryReader/Writer. You will use a combination of all three in your project.
Methods in the FileStream Class: The File denotes the Path and name of the file to be used and the FileMode. File.OpenRead - opens file for reading File.Open - Open file for writing with a following mode FileMode.Create - Opens file and overwrites existing file if present FileMode.CreateNew - Opens file and throws exception if file already exists FileMode.Append - Opens file and moves to end of file for writing
Methods in the StreamWriter Class: - Close - Closes the current StreamWriter object and the underlying stream - Flush - Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream - Write - Writes to the stream - WriteLine - Writes data specified by the overloaded parameters, followed by end of line
Methods in the StreamReader Class: Close - Closes the object of StreamReader class and the underlying stream, and release any system resources associated with the reader Peek - Returns the next available character but doesn't consume it Read - Reads the next character or the next set of characters from the stream Read Line - Reads a line of characters from the current stream and returns data as a string Seek - Allows the read/write position to be moved to any position with the file
Binary Files: This example consists of two programs: The first reads values for book records from the keyboard and saves them in a binary file. The second reads the binary file and displays the values on the console. You may wish to read records from a file into an array of records while the program is running and store the contents of the array back into the file before the program terminates.
Object Files - Serialisation You can also save entire objects and their state e.g. fields and values, to a file through serialization. Serialization is known as the process of flattening out an object so that it can be saved. To read the file back in, it is de-serialized, i.e. re-inflated, so that it is restored to its original state. When objects are serialized, they are stored in binary format so they need to be able to write binary out to a file and read binary in from a file. The BinaryReader and BinaryWriter classes are used for this purpose.
Error trapping File-handling operations should always be protected with the appropriate exception-handling code because the file to be read from or written to might be missing, in a different location, renamed etc. The methods: IOException, SerializationException could be used to prevent run-time errors. *Remember to Close the File, Stream and Binary Readers as this releases the file to be accessed by other processes/programs. Again, not doing so could cause a run-time error.
Show full summary Hide full summary

Similar

Cell Structure
megan.radcliffe16
Exchange surfaces and breathing
megan.radcliffe16
OCR AS Biology
joshbrown3397
Random German A-level Vocab
Libby Shaw
Psychology subject map
Jake Pickup
AQA AS Biology Unit 2 DNA and Meiosis
elliedee
Biology Unit 1
hannahsanderson1
Economics - unit 1
Amardeep Kumar
Cells And Cell Techniques - Flashcards (AQA AS-Level Biology)
Henry Kitchen
PSYA1 - attachment, AQA psychology
T W
Biological Psychology - Stress
Gurdev Manchanda