0% found this document useful (0 votes)
6 views

2 OOP FileHandling

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

2 OOP FileHandling

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Object Oriented Programming (OOP)

File handling in C#

Dr. Rida El Chall

Lebanese University – Faculty of Engineering

3rd year – Semester 5


2024 - 2025
1
File handling
▪ A file is a collection of data stored on a disk with a specific name, extension, and
directory path.
▪ When you open File using C# for reading and writing purposes it becomes Stream.
▪ A stream is a sequence of bytes travelling from a source to a destination over a
communication path.
▪ Input Stream: This Stream is used to read data from a file, which is known as a read
operation.
▪ Output Stream: This Stream is used to write data into a file, which is known as a
write operation.

▪ System.IO namespace provides classes for file and stream handling

3rd year - Electrical Engineering Basic Concepts of OOP 2


File Handling

▪ The FileInfo, DirectoryInfo, and DriveInfo classes have instance methods.


▪ File, Directory, and Path classes have static methods.

3rd year - Electrical Engineering Basic Concepts of OOP 3


File Handling

• File - provides static methods for creating, copying, deleting, moving, and opening files, and
helps create a FileStream object.

• FileInfo - provides instance methods for creating, copying, deleting, moving, and opening
files, and helps create a FileStream object.

• Directory - provides static methods for creating, moving, and enumerating through
directories and subdirectories.

• DirectoryInfo - provides instance methods for creating, moving, and enumerating through
directories and subdirectories.

• Path - provides methods and properties for processing directory strings in a cross-platform
manner.

3rd year - Electrical Engineering Basic Concepts of OOP 4


File Class
▪ Provides static methods for the creation, copying, deletion, moving, and opening of a single
file, and aids in the creation of FileStream objects.
▪ Many of the File methods return other I/O
types when you create or open files.
Open, OpenRead, OpenText, CreateText, or Create.

▪ File.Exists(string path): Checks if a file exists


▪ File.Open(string path, FileMode mode): Opens a file in the
specified mode (e.g.,Append). return a FileStream
▪ File.Create(string path): Creates a new file at the specified
path, return a FileStream
▪ File.Delete(string path): Deletes the file at the specified path.
▪ File.ReadAllText(string path): Reads the contents of a text file
into a string.
▪ File.WriteAllText(string path, string contents): Writes the
specified string to a text file.
3rd year - Electrical Engineering Basic Concepts of OOP 5
FileInfo Class
▪ Provides properties and instance methods for the creation, copying, deletion, moving,
and opening of files, and aids in the creation of FileStream objects. This class cannot be
inherited.

▪ Many of the FileInfo methods return


other I/O types when you create or open
files. Open, OpenRead, OpenText, CreateText,
or Create.

▪ Because all File methods are static, if you want to


perform only one action (quick file access), it
might be more efficient to use a File method than
a corresponding FileInfo instance method.

3rd year - Electrical Engineering Basic Concepts of OOP 6


FileStream Class
▪ Provides a Stream for a file, supporting both synchronous and asynchronous read and
write operations.

string filepath = “tmp.txt";


FileStream a = new FileStream(filepath,
FileMode.Open, FileAccess.Read);

3rd year - Electrical Engineering Basic Concepts of OOP 7


StreamReader Class
▪ StreamReader: implements a TextReader that reads characters from a byte stream in a
particular encoding.
▪ StringReader: implements a TextReader that reads from a string.
TextReader is the abstract base class of StreamReader and StringReader

3rd year - Electrical Engineering Basic Concepts of OOP 8


StreamWriter Class
▪ StreamWriter: Implements a TextWriter for writing characters to a stream in a particular
encoding.
▪ StringWriter : implements a TextWriter for writing information to a string

3rd year - Electrical Engineering Basic Concepts of OOP 9


Directory and DirectoryInfo
▪ Directory: Static methods for manipulating directories (e.g., creating, moving, deleting).
▪ DirectoryInfo: Instance-based class that represents a specific directory and allows you to
perform operations on it.

// Directory Example
string dirPath = "NewFolder";
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}

// DirectoryInfo Example
DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
FileInfo[] files = dirInfo.GetFiles();

3rd year - Electrical Engineering Basic Concepts of OOP 10


How to: Write text to a file
▪ Different ways to write:
• StreamWriter contains methods to write to a file synchronously (Write and WriteLine) or
asynchronously (WriteAsync and WriteLineAsync).

• File provides static methods to write text to a file such as WriteAllLines and WriteAllText,
or to append text to a file such as AppendAllLines, AppendAllText, and AppendText.

3rd year - Electrical Engineering Basic Concepts of OOP 11


Read text from a file

3rd year - Electrical Engineering Basic Concepts of OOP 12


Serialization
▪ Serialization is the process of converting an object or data structure into a format that
can be easily stored or transmitted.
• System.Runtime.Serialization
• Deserialization is the reverse process, where data in these formats is converted back into
an object.
• Serialization is essential for saving application state, sending data over a network, or
storing complex objects in files or databases.
• . Types of Serialization in C#
• Binary Serialization: BinaryFormatter replacements like protobuf-net or MessagePack
• XML Serialization (useful for configuration files and legacy systems); XmlSerializer
• JSON Serialization (ideal for web and API applications) ; System.Text.Json

3rd year - Electrical Engineering Basic Concepts of OOP 13


Binary Serialization

•Binary serialization with


BinaryFormatter can be
dangerous

3rd year - Electrical Engineering Basic Concepts of OOP 14


JSON Serialization
▪ JSON serialization is popular in modern applications, particularly for web APIs. It’s
efficient and human-readable, and JSON is a widely accepted data interchange format.

3rd year - Electrical Engineering Basic Concepts of OOP 15

You might also like