chapter 14: files and streams. 2microsoft visual c# 2012, fifth edition files and the file and...
TRANSCRIPT
![Page 1: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/1.jpg)
Chapter 14:Files and Streams
![Page 2: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/2.jpg)
2Microsoft Visual C# 2012, Fifth Edition
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
![Page 3: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/3.jpg)
3Microsoft Visual C# 2012, Fifth Edition
Files and the File and Directory Classes (cont’d.)
• 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.
![Page 4: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/4.jpg)
4Microsoft Visual C# 2012, Fifth Edition
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
![Page 5: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/5.jpg)
5Microsoft Visual C# 2012, Fifth Edition
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
![Page 6: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/6.jpg)
6Microsoft Visual C# 2012, Fifth Edition
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
![Page 7: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/7.jpg)
7Microsoft Visual C# 2012, Fifth Edition
Using the File and Directory Classes (cont’d.)
![Page 8: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/8.jpg)
8Microsoft Visual C# 2012, Fifth Edition
![Page 9: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/9.jpg)
9Microsoft Visual C# 2012, Fifth Edition
Using the File and Directory Classes (cont’d.)
![Page 10: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/10.jpg)
10Microsoft Visual C# 2012, Fifth Edition
Using the File and Directory Classes (cont’d.)
![Page 11: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/11.jpg)
11Microsoft Visual C# 2012, Fifth Edition
![Page 12: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/12.jpg)
12Microsoft Visual C# 2012, Fifth Edition
Using the File and Directory Classes (cont’d.)
![Page 13: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/13.jpg)
13Microsoft Visual C# 2012, Fifth Edition
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
![Page 14: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/14.jpg)
14Microsoft Visual C# 2012, Fifth Edition
Understanding File Data Organization (cont’d.)
![Page 15: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/15.jpg)
15Microsoft Visual C# 2012, Fifth Edition
• 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
Understanding File Data Organization (cont’d.)
![Page 16: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/16.jpg)
16Microsoft Visual C# 2012, Fifth Edition
• 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
Understanding File Data Organization (cont’d.)
![Page 17: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/17.jpg)
17Microsoft Visual C# 2012, Fifth Edition
Understanding Streams
• Stream– Functions as a pipeline or channel between an input device
and an application, and potentially an output device
![Page 18: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/18.jpg)
18Microsoft Visual C# 2012, Fifth Edition
Understanding Streams (cont’d.)
• Most streams flow in only one direction
– StreamReader for text input from a file– StreamWriter for text output to a file– FileStream for both input from and output to a file
![Page 19: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/19.jpg)
19Microsoft Visual C# 2012, Fifth Edition
Understanding Streams (cont’d.)
![Page 20: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/20.jpg)
20Microsoft Visual C# 2012, Fifth Edition
Understanding Streams (cont’d.)
![Page 21: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/21.jpg)
21Microsoft Visual C# 2012, Fifth Edition
Understanding Streams (cont’d.)
![Page 22: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/22.jpg)
22Microsoft Visual C# 2012, Fifth Edition
Understanding Streams (cont’d.)
![Page 23: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/23.jpg)
23Microsoft Visual C# 2012, Fifth Edition
• 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
Writing Data to a Sequential Access Text File
![Page 24: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/24.jpg)
24Microsoft Visual C# 2012, Fifth Edition
Writing Data to a Sequential Access Text File (Using Classes)
![Page 25: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/25.jpg)
25Microsoft Visual C# 2012, Fifth Edition
![Page 26: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/26.jpg)
26Microsoft Visual C# 2012, Fifth Edition
Writing Data to a Sequential Access Text File (cont’d.)
![Page 27: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/27.jpg)
27Microsoft Visual C# 2012, Fifth Edition
Writing Data to a Sequential Access Text File (cont’d.)
![Page 28: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/28.jpg)
28Microsoft Visual C# 2012, Fifth Edition
Reading from a Sequential Access Text File
• Reading from a text file is similar to writing to a text file
• Classes used:– FileStream– StreamReader
![Page 29: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/29.jpg)
29Microsoft Visual C# 2012, Fifth Edition 29
![Page 30: Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer](https://reader035.vdocument.in/reader035/viewer/2022062407/56649f4e5503460f94c6fcad/html5/thumbnails/30.jpg)
30Microsoft Visual C# 2012, Fifth Edition
Reading from a Sequential Access Text File (cont’d.)