Fundamentals of Data Structures File Handling Computer Science





























- Slides: 29

Fundamentals of Data Structures File Handling Computer Science

File Handling • One very important aspect of a program is its ability to permanently save data to a file – This means file handling I. e. outputting to a file, and getting input from a file • There are two terms for this: Output to a file: Save Input from a file: Load • In either case, we first need to open the file and perform the correct task on it • Once we’re done, we need to close the file Keeping a file open locks it by this program That means no other program can open it in the meantime 2 Fundamentals of Data Structures: File Handling

File Handling • Whenever we open a file, we typically state why we’re opening the file – As in, what mode are we opening the file in • We have three different modes when opening a file 1. Write: writing (and overwriting) data to the file 2. Append: writing (adding near the bottom) data to the file 3. Reading: reading data from the file • We also have two possibilities when it comes to writing to a file 1. The file doesn’t exist (we’re making it right now) 2. The file does exist (we’re overwriting it, or adding to it) 3 Fundamentals of Data Structures: File Handling

File Handling: Creating Files • The first possibility we will deal with is saving data to a file But no file has been made so far • What we’re essentially doing here is making a new file at the same time • This means we need to have the data in a format ahead of time – Usually text-based – However, will also look at CSV (Comma Separated Values) 4 Fundamentals of Data Structures: File Handling

File Handling: Creating Files • Here’s an example of creating a file and writing some text to it Python Java Note that only Python has a mode character (‘w’ for ‘write’). C# 5 In C# and Java, the mode is represented by the Stream. Writer and the File. Writer. Fundamentals of Data Structures: File Handling

• Create a new program which does the following 1. Asks for the user’s name and age 2. Creates a new text file and saves both of these to it • If we run this program multiple times, it’ll simply overwrite the content that was in it originally • Run your program multiple times, checking the text file after each run! 6 Fundamentals of Data Structures: File Handling

File Handling: Appending to Files • If we want to repeatedly add to a file, then we don’t use the write mode • We use the append mode • For example, we could make a program which logs the user’s name Each time the program is run • Writing would overwrite this log every time, so we need to use append 7 Fundamentals of Data Structures: File Handling

File Handling: Appending to Files • Here’s an example of creating a file and writing some text to it Python Java For Python, we change the “w” to “a”. For C#, we add true when making the Stream. Writer. For Java, we replace the write() call with append(). C# Note that this still works even if the file doesn’t exist yet! 8 Fundamentals of Data Structures: File Handling

• Change your program from the previous exercise so that it appends to the file instead! • Don’t forget to add a newline character at some point Otherwise the text in the file won’t move on to the next line 9 Fundamentals of Data Structures: File Handling

File Handling: Reading from a File • Finally, we need to see how we can read from a file Could be a save file for a game Or the name of the user who previously used the program • In Python, we switch the mode to r (for read) and use a different function • For C# and Java, we need to use different objects Readers rather than Writers 10 Fundamentals of Data Structures: File Handling

File Handling: Reading from a File • Here’s how we read from a file Python Java Python and C# have simple ways of reading all the content from a file. In Java, however, we read it a line at a time. C# This is using the Scanner (the same thing we use to read input from the console)! 11 Fundamentals of Data Structures: File Handling

• In the same program you made earlier (after append data to the file) • Read the content from the file and output it to the console • In Python and C#, you can use a simple function for reading all the content • In Java, you need to read the file one line at a time 12 Fundamentals of Data Structures: File Handling

Plaintext and CSV Files • Everything we’ve written to so far has been plaintext – The file contains only ASCII characters – With no other formatting • However, sometimes we want to store information in a bit more of a structured way – For example, we want to store a table (with columns and rows) • A plaintext file isn’t suited to that – So then what is? 13 Fundamentals of Data Structures: File Handling

CSV Files • That’s where Comma Separated Value (CSV) files come in! • They are still plaintext based, but they have a bit more formatting • In CSV files, we separate different columns using commas And different rows using newlines • The great thing is that CSV files are supported by a lot of table management software Like Microsoft Excel! 14 Fundamentals of Data Structures: File Handling

CSV Files • For example, we may be trying to store high scores in a video game – We need to remember the user’s name, their score, and the date they got that score on • We can store these values for each user on different lines Data in CSV file Name, Score, Date Jason, 123000, 02/05/2026 Emily, 101000, 04/01/2026 Sarah, 98000, 15/03/2026 15 Opened in Excel Name Score Date Jason 123000 02/05/2026 Emily 101000 04/01/2026 Sarah 98000 15/03/2026 Fundamentals of Data Structures: File Handling

CSV Files • The trick with saving CSV files is to add a comma after every attribute – And a newline after every record • However, we also need to read/load them differently – As we need to read them one line at a time (for each record) – Then split that line based on the commas (for each attribute) • Sometimes, when saving a CSV file, a blank newline can be found at the end – We will also need to stop this from happening – It could crash the program when loading (as we try and split an empty string) 16 Fundamentals of Data Structures: File Handling

CSV Files: Saving • Here’s an example of saving to a CSV file Java • In Java, when writing using a File. Writer, we need to convert any numbers into a String – Otherwise it thinks of the number in ASCII/Unicode 17 Fundamentals of Data Structures: File Handling

CSV Files: Saving • Here’s an example of saving to a CSV file C# 18 Fundamentals of Data Structures: File Handling

CSV Files: Saving • Here’s an example of saving to a CSV file Python 19 Fundamentals of Data Structures: File Handling

CSV File: Loading Java • And here’s an example of loading a CSV file • Here we’re working through the file twice – Once to work out how many lines there are (for the array sizes) – Twice to move the values in the file into the correct place in the array 20 Fundamentals of Data Structures: File Handling

CSV File: Loading • And here’s an example of loading a CSV file C# 21 Fundamentals of Data Structures: File Handling

CSV Files: Saving • And here’s an example of loading a CSV file Python • As Python uses lists for everything, we don’t need to read through the file twice – We simply add the attributes to the lists as needed 22 Fundamentals of Data Structures: File Handling

• Create a CSV file (programmatically) that contains values for – Name (string) – Test Result (integer between 0 and 100) – Grade (string from F to A, though you can decide what result gets what grade) • Make at least three records with each of these attributes and write them to the CSV file – Make a procedure to do this • Make another procedure for reading from this CSV file – And storing the results into three arrays/lists – You may need to make these arrays global to get their values out of the procedure 23 Fundamentals of Data Structures: File Handling

Binary Files • The only other file type we would need to think about are binary files • These aren’t text-based files – Like plaintext or CSV files • They are the most pure file types, as they only contain binary data – 1 s and 0 s! • When writing/reading with these file-types, we need to encode/decode whatever data we’re dealing with 24 Fundamentals of Data Structures: File Handling

Binary Files • We’re going to make a string value and get its binary data – In the form of a byte array • Then we’ll write this information to file – Using a different file-extension than usual – We’ll make our own – called Binary Data File (. bdf) 25 Fundamentals of Data Structures: File Handling

Binary Files: Writing • Here’s an example of writing a binary data file There a few changes here Python In all three languages, we need to convert the string into a byte array (sometimes specifying the encoding to use, like UTF-8). Java In Python, we use the usual method except for the added “b” in the open mode (which means binary). In Java and C#, we can use a separate method for writing binary. C# 26 Fundamentals of Data Structures: File Handling

Binary Files: Writing • And here’s reading byte data from a file Python Java C# 27 Fundamentals of Data Structures: File Handling

1. Make a string phrase and convert it to a byte array a. Write this byte array to a file b. Using a file-extension of your choice 2. Run this program, and try opening the file with a text-editor Can you read the data in it? 3. Now try reading the data from the file into a byte array a. Convert it into a string b. Then output it to the console 28 Fundamentals of Data Structures: File Handling
