Presentation is loading. Please wait.

Presentation is loading. Please wait.

MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.

Similar presentations


Presentation on theme: "MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition."— Presentation transcript:

1 MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition

2 Objectives Learn about computer files and the File and Directory classes Understand file data organization Understand streams Write to and read from a sequential access text file Search a sequential access text file Understand serialization and deserialization 2Microsoft Visual C# 2012, Fifth Edition

3 Files and the File and Directory Classes Temporary storage – Usually called computer memory or random access memory (RAM) – Variables use temporary storage – Volatile Permanent storage – Data is not lost when a computer loses power – Nonvolatile – The program is saved to a disk 3Microsoft Visual C# 2012, Fifth Edition

4 Files and the File and Directory Classes (cont’d.) Computer file – A collection of information stored on a nonvolatile device in a computer system – Files exist on permanent storage devices Text files – Contain information in ASCII or Unicode characters Can be read in a plain text editor Can be data files or source code files (e.g., C# source code) Binary files – Store software, images, music, etc. 4Microsoft Visual C# 2012, Fifth Edition

5 Files and the File and Directory Classes (cont’d.) Characteristics of a file – Occupies space on a section of a storage device – Has a name, a size, a type, and a specific time of creation Write to the file – Store data in a file on a persistent storage device Read from the file – Copy data from a file on a storage device into RAM Computer users organize their files into folders or directories 5Microsoft Visual C# 2012, Fifth Edition

6 Files and the File and Directory Classes (cont’d.) Path – A combination of the disk drive plus the complete hierarchy of directories in which a file resides Example: C:\C#\Chapter.14\Data.txt C# provides built-in classes named File and Directory – Contain methods to help you manipulate files and their directories Access information about files Create, delete, or move files 6Microsoft Visual C# 2012, Fifth Edition

7 Using the File and Directory Classes File class – Contains methods to access information about files – Contained in the System.IO namespace Directory class – Provides information about directories or folders 7Microsoft Visual C# 2012, Fifth Edition

8 Using the File and Directory Classes (cont’d.) 8Microsoft Visual C# 2012, Fifth Edition

9 9

10 10Microsoft Visual C# 2012, Fifth Edition Using the File and Directory Classes (cont’d.)

11 11Microsoft Visual C# 2012, Fifth Edition Using the File and Directory Classes (cont’d.)

12 12Microsoft Visual C# 2012, Fifth Edition

13 13Microsoft Visual C# 2012, Fifth Edition Using the File and Directory Classes (cont’d.)

14 Understanding File Data Organization Businesses store data in a relationship known as the data hierarchy Character – Any of the letters, numbers, or other special symbols (such as punctuation marks) that comprise data – Characters are made up of bytes containing eight (8) bits ASCII characters contain one (1) byte Unicode characters contain two (2) bytes 14Microsoft Visual C# 2012, Fifth Edition Why?

15 Understanding File Data Organization (cont’d.) 15Microsoft Visual C# 2012, Fifth Edition

16 Field – A character or group of characters that has some meaning Record – A collection of related fields that contain data about an entity Data files – Consist of related records Sequential access file – Each record in the file is read in order based on its relative position – Records can be stored in order based on a value in the record 16Microsoft Visual C# 2012, Fifth Edition Understanding File Data Organization (cont’d.)

17 A C# application opens a file by creating an object and associating a stream of bytes with that object When you finish using a file, the program should close the file – Not closing a file may make it inaccessible – Not closing an output file can result in data not being written to the file 17Microsoft Visual C# 2012, Fifth Edition Understanding File Data Organization (cont’d.)

18 Understanding Streams Stream – Functions as a pipeline or channel between an input device and an application, and potentially an output device 18Microsoft Visual C# 2012, Fifth Edition

19 Understanding Streams (cont’d.) 19Microsoft Visual C# 2012, Fifth Edition Default stream objects in C#: – Console.In Accepts data from the keyboard – Console.Out Allows a program to produce output on the screen – Console.Error Allows a program to write error messages to the screen Since we create C# programs that interface with forms, we will not use the Console for input or output

20 Understanding Streams (cont’d.) Most streams flow in only one direction File processing classes include: – StreamReader for text input from a file – StreamWriter for text output to a file – FileStream for both input from and output to a file 20Microsoft Visual C# 2012, Fifth Edition

21 Understanding Streams (cont’d.) 21Microsoft Visual C# 2012, Fifth Edition

22 Understanding Streams (cont’d.) 22Microsoft Visual C# 2012, Fifth Edition

23 Understanding Streams (cont’d.) 23Microsoft Visual C# 2012, Fifth Edition

24 Understanding Streams (cont’d.) 24Microsoft Visual C# 2012, Fifth Edition

25 Writing and Reading a Sequential Access File C# uses files only as streams of bytes When you write a program to store a data file, you must dictate the form the file will take 25Microsoft Visual C# 2012, Fifth Edition

26 Delimiter – A character used to specify the boundary between records and, potentially, fields in text files When you write data to a text file: – You can separate the fields with a delimiter – Delimiters are needed when fields are not fixed in size and position—field size varies – CSV files (comma-separated value files) are delimited files 26Microsoft Visual C# 2012, Fifth Edition Writing Data to a Sequential Access Text File

27 27Microsoft Visual C# 2012, Fifth Edition Writing Data to a Sequential Access Text File (cont’d.)

28 28Microsoft Visual C# 2012, Fifth Edition

29 29Microsoft Visual C# 2012, Fifth Edition Writing Data to a Sequential Access Text File (cont’d.)

30 30Microsoft Visual C# 2012, Fifth Edition Writing Data to a Sequential Access Text File (cont’d.)

31 Reading from a Sequential Access Text File Reading from a text file is similar to writing to a text file Classes used: – FileStream – StreamReader 31Microsoft Visual C# 2012, Fifth Edition

32 32 Microsoft Visual C# 2012, Fifth Edition 32

33 Reading from a Sequential Access Text File (cont’d.) 33Microsoft Visual C# 2012, Fifth Edition

34 Searching a Sequential Text File File position pointer – Holds the byte number of the next byte to be read Reposition the file pointer using the Seek() method and the SeekOrigin enumeration 34Microsoft Visual C# 2012, Fifth Edition

35 Searching a Sequential Text File (cont’d.) 35Microsoft Visual C# 2012, Fifth Edition

36 36Microsoft Visual C# 2012, Fifth Edition

37 Searching a Sequential Text File (cont’d.) 37Microsoft Visual C# 2012, Fifth Edition

38 Understanding Serialization and Deserialization Disadvantages to writing to a text file: – Data in a text file is easily readable in a text editor such as Notepad, which means it is not secure – It is cumbersome to convert each field to text and combine the fields with delimiters You can write objects to and read objects from files Serialization – The process of converting objects into streams of bytes Deserialization – Converts streams of bytes back into objects 38Microsoft Visual C# 2012, Fifth Edition

39 Understanding Serialization and Deserialization (cont’d.) 39Microsoft Visual C# 2012, Fifth Edition

40 Understanding Serialization and Deserialization (cont’d.) To create a class that can be serialized: – Mark it with the [Serializable] attribute – Every instance variable must also be serializable C# simple data types are serializable, including string s Namespaces included in programs that employ serialization: – System.Runtime.Serialization.Formatters. Binary; – System.Runtime.Serialization; 40Microsoft Visual C# 2012, Fifth Edition

41 41Microsoft Visual C# 2012, Fifth Edition

42 Understanding Serialization and Deserialization (cont’d.) 42Microsoft Visual C# 2012, Fifth Edition

43 Understanding Serialization and Deserialization (cont’d.) 43Microsoft Visual C# 2012, Fifth Edition

44 You Do It Creating a File Reading from a File Using the Seek() Method Creating a Text File in a GUI Environment Reading Data from a Text File into a Form 44Microsoft Visual C# 2012, Fifth Edition

45 Summary Temporary storage is usually called computer memory or random access memory (RAM) A character can be any one of the letters, numbers, or other special characters that comprise data Bytes flow into and out of applications through streams 45Microsoft Visual C# 2012, Fifth Edition

46 Summary Data can be written to a StreamWriter object using the WriteLine() method Data can be read from a StreamReader object using the ReadLine() method When you read data from a sequential file, subsequent records are read in order 46Microsoft Visual C# 2012, Fifth Edition


Download ppt "MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition."

Similar presentations


Ads by Google