Podcast
Questions and Answers
What are files used for in computing?
What are files used for in computing?
C# uses the Unicode character set, where characters are composed of 8 bytes.
C# uses the Unicode character set, where characters are composed of 8 bytes.
False
What is a field in the context of data hierarchy?
What is a field in the context of data hierarchy?
A group of characters that conveys meaning
A record is a collection of related __________ that contain data about an entity.
A record is a collection of related __________ that contain data about an entity.
Signup and view all the answers
Match the following stream types with their description:
Match the following stream types with their description:
Signup and view all the answers
Which class is used to write binary data to a file in C#?
Which class is used to write binary data to a file in C#?
Signup and view all the answers
In C#, you cannot view the contents of a binary file using Notepad.
In C#, you cannot view the contents of a binary file using Notepad.
Signup and view all the answers
The constructor for the FileStream object includes values for two enumerated types: FileMode.Open and FileAccess._.
The constructor for the FileStream object includes values for two enumerated types: FileMode.Open and FileAccess._.
Signup and view all the answers
Match the methods in the BinaryReader class with their descriptions:
Match the methods in the BinaryReader class with their descriptions:
Signup and view all the answers
Which class provides static methods for creating, copying, deleting, moving, and opening files, and aids in the creation of FileStream objects?
Which class provides static methods for creating, copying, deleting, moving, and opening files, and aids in the creation of FileStream objects?
Signup and view all the answers
What is the purpose of the StreamWriter class?
What is the purpose of the StreamWriter class?
Signup and view all the answers
Enclose attempts to access text files inside ______ blocks.
Enclose attempts to access text files inside ______ blocks.
Signup and view all the answers
The StreamWriter class has a method called AutoClose to close the stream.
The StreamWriter class has a method called AutoClose to close the stream.
Signup and view all the answers
Match the StreamWriter method with its description:
Match the StreamWriter method with its description:
Signup and view all the answers
To invoke a method in C#, the method name is preceded by the _______ name.
To invoke a method in C#, the method name is preceded by the _______ name.
Signup and view all the answers
What additional functionality do DirectoryInfo and FileInfo classes provide beyond File and Directory classes?
What additional functionality do DirectoryInfo and FileInfo classes provide beyond File and Directory classes?
Signup and view all the answers
What are some of the key properties of DirectoryInfo class?
What are some of the key properties of DirectoryInfo class?
Signup and view all the answers
Serialization is the process of converting objects into streams of bytes.
Serialization is the process of converting objects into streams of bytes.
Signup and view all the answers
What purpose does the BinaryFormatter class serve in C#?
What purpose does the BinaryFormatter class serve in C#?
Signup and view all the answers
Match the FileInfo properties with their descriptions:
Match the FileInfo properties with their descriptions:
Signup and view all the answers
Study Notes
Introduction to Files in C#
- Files are used for long-term retention of large amounts of data, even after the program terminates.
- Data maintained in files is called persistent data.
- Files are stored on secondary storage devices such as magnetic disks, optical disks, flash memory, and magnetic tapes.
Data Hierarchy
- A bit is the smallest data item that computers support.
- Bytes are composed of eight bits.
- C# uses the Unicode character set, where characters are composed of 2 bytes.
- A field is a group of characters that conveys meaning.
- Records are collections of related fields that contain data about an entity.
- Files are a group of related records.
File Types
- Sequential Access Files: Each record is read in order based on its relative position.
- Random Access Files: Records are not in any sequence and can be accessed in any order.
Files and Streams
- C# views each file as a sequential stream of bytes.
- Each file ends either with an end-of-file marker or at a specific byte number that is recorded in a system-maintained administrative data structure.
Understanding Streams
- A stream functions as a pipeline or channel between an input device and an application, and potentially an output device.
- Streams can be read from and written to other streams, adding additional functionality (e.g., buffering, encoding, and compression).
Types of Streams in .NET Framework
- Base Streams: Read and write data from and to an external data storage mechanism.
- Pass-through Streams: Read from and write to other streams, adding additional functionality (e.g., buffering, encoding, and compression).
Main Operations with Streams
- Construct (Create): Connects to the data transfer/storage mechanism or to another stream.
- Reading: Retrieves data from the stream.
- Writing: Sends data to the stream.
- Positioning: Moves the current stream position (if positioning is supported).
- Closure: Completes the work on the stream and releases the used resources.
System.IO Classes
- BinaryReader: Reads primitive data types as binary values.
- BinaryWriter: Writes primitive types in binary to a stream.
- Directory: Exposes static methods for creating and moving through directories and subdirectories.
- DirectoryInfo: Exposes instance methods for creating and moving through directories and subdirectories.
Readers and Writers
- TextReader: Represents a reader that can read a sequential series of characters.
- TextWriter: Represents a writer that can write a sequential series of characters.
- StreamReader: Reads text data from a stream.
- StreamWriter: Writes text data to a stream.
File Streams
- File Streams are used to read and write data to and from files.
- StreamReader and StreamWriter classes are used to read and write text data to and from files.
Writing Text Files
- Use the StreamWriter class to write text data to a file.
- Use the Close() method to finish storing values.
StreamReader and StreamWriter Members
-
StreamReader Members: Close(), DiscardBufferedData(), Dispose(), Peek(), Read(), ReadBlock(), ReadLine(), ReadToEnd().
-
StreamWriter Members: AutoFlush (Property), Close(), Dispose(), Flush(), NewLine (Property), Write(), WriteLine().### File Streams
-
The
FileStream
class is used to work with file streams, inherits from theStream
class, and supports all its methods and properties. -
It supports reading, writing, positioning (if the device where the file is located supports these operations).
-
In its constructor, it is set: file name, way to open the file, access mode, and access for competing users.
Constructing a File Stream
-
FileStream
constructor:FileStream fs = new FileStream(string fileName, FileMode [, FileAccess [, FileShare]]);
-
FileMode
: file opening mode (Open, Append, Create, CreateNew, OpenOrCreate, Truncate) -
FileAccess
: file access mode (Read, Write, ReadWrite) -
FileShare
: access mode for other users while we keep the file open (None, Read, Write, ReadWrite)
BinaryReader and BinaryWriter Classes
- Used for writing and reading binary data, rather than character strings.
- Files created are readable by the computer but cannot be opened and read using Notepad.
- A program is needed to interpret the file contents.
BinaryWriter Class
- Instantiates objects of the
FileStream
andBinaryWriter
classes. -
FileStream
object is then sent as an argument to theBinaryWriter
constructor.
BinaryReader Class
- Cannot simply open a binary file in Notepad and view its contents.
- Need to write program statements that use the
BinaryReader
class to retrieve the results. - Constructor for the
FileStream
object includes values for two enumerated types:FileMode.Open
andFileAccess.Read
.
File and Directory Classes
-
File
class: contains methods to access information about files, contained in theSystem.IO
namespace. -
Directory
class: provides information about directories or folders.
File and FileInfo Classes
- Provide functionality to: Create file, Open a file, Copy file, Move (rename) a file, Delete file, Extract the time of last access and modification, Existence check.
- The
File
class provides these methods statically andFileInfo
by instance.
Directory and DirectoryInfo Classes
- Provide functionality to: Create directories and subdirectories, Retrieve all files, Retrieve all subdirectories, Move (rename), Delete, Extract the parent directory, Existence check, Extract the full name.
- The
Directory
class provides these methods statically andDirectoryInfo
by instance.
File Class Static Members and Their Descriptions
-
AppendAllText()
: Appends the specified string to the file, creating the file if it does not already exist. -
AppendText()
: Creates aStreamWriter
that appends UTF-8 encoded text to an existing file or creates a file if one does not exist. -
Copy()
: Copies an existing file to a new file. -
Create()
: Creates a file in the specified path and returns its associatedStreamWriter
. -
Delete()
: Deletes the specified file. -
Exists()
: Determines whether the specified file exists.
Directory Class Public Static Members and Their Descriptions
-
CreateDirectory()
: Creates all the directories in a specified path. -
Delete()
: Deletes a specified directory. -
Exists()
: Determines whether the given path refers to an existing directory. -
GetCreationTime()
: Gets the creation date and time of a directory. -
GetCurrentDirectory()
: Gets the current working directory of the application.
Utility Classes
-
Utility classes allow you to manipulate files and directory structures.
-
Aid in copying, moving, renaming, creating, opening, deleting, and appending files.
-
Expose only static members.
-
Objects are not instantiated from these classes.
-
To invoke the method, the method name is preceded by the class name (as opposed to an object’s name).### Environment Special Folders
-
Environment.GetFolderPath
method is used to retrieve the path of special folders. -
Some special folders include
DesktopDirectory
,MyDocuments
,Favorites
, andMyMusic
. -
The method returns a string representing the path of the specified special folder.
Understanding Serialization and Deserialization
- Serialization is the process of converting objects into streams of bytes.
- Deserialization is the process of converting streams of bytes back into objects.
- Disadvantages of writing to a text file include:
- Data is easily readable and not secure.
- Converting each field to text and combining fields with delimiters is cumbersome.
Object Serialization in C#
- Object serialization allows entire objects to be written to or read from a file.
- A serialized object is an object represented as a sequence of bytes that includes the object's data, its type, and the types of data stored in the object.
BinaryFormatter Class
- The
BinaryFormatter
class enables entire objects to be written to or read from a stream. - The
Serialize
method writes an object's representation to a file. - The
Deserialize
method reads the representation from a file and reconstructs the original object. - Both methods throw a
SerializationException
if an error occurs during serialization or deserialization.
Example Serializable Class
- A class must be marked with the
[Serializable]
attribute to be serializable. - In a serializable class, every instance variable must also be serializable.
- Example of a serializable class:
-
[Serializable]
classEmployee
with propertiesEmpNum
,Name
, andSalary
.
-
Serializable Class Requirements
- In a serializable class, every instance variable must also be serializable.
- By default, all C# simple data types are serializable, including strings.
- If a class contains fields that are more complex data types, they must be checked to ensure they are serializable.
- Namespaces required for serialization:
-
System.Runtime.Serialization.Formatters.Binary
-
System.Runtime.Serialization
-
Serialization Example
- Example of serializing an
Employee
object to a file and deserializing it back. - Using
FileStream
to create and read from a file. - Using
BinaryFormatter
to serialize and deserialize the object.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Learn about files in C# programming, including data hierarchy and storage. Understand bits, bytes, and Unicode character set in C#.