Chapter Five

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Werabe University

Institute of Technology

Event Driven Programming


Manipulating Files
Introduction

• A file is a collection of data stored in a disk with a specific name and


a directory path. When a file is opened for reading or writing, it
becomes a stream.

• The stream is basically the sequence of bytes passing through the


communication path. There are two main streams: the input stream
and the output stream.
Cont..

• The input stream is used for reading data from file (read operation)
and the output stream is used for writing into the file (write
operation).

• C# I/O Classes: The System. IO namespace has various classes that are
used for performing numerous operations with files, such as creating
and deleting files, reading from or writing to a file, closing a file etc.
Cont..

• The following table shows some commonly used non-abstract


classes in the System. IO namespace:
Cont..
The FileStream Class
• The FileStream class in the System. IO namespace helps in reading
from, writing to and closing files. This class derives from the
abstract class Stream.

• You need to create a FileStream object to create a new file or open


an existing file. The syntax for creating a FileStream object is as
follows:
Cont..

• For example, we create a FileStream object F for reading a file


named sample.txt as shown:
Cont..

• FileMode: The FileMode enumerator defines various methods for opening files.
The members of the FileMode enumerator are:

• Append: It opens an existing file and puts cursor at the end of file, or
creates the file, if the file does not exist.

• Create: It creates a new file.

• CreateNew: It specifies to the operating system, that it should create a

• new file
Cont..

•Open: It opens an existing file.

•OpenOrCreate: It specifies to the operating system that it should


open a file if it exists, otherwise it should create a new file.

•Truncate: It opens an existing file and truncates its size to zero


bytes.
Cont..
• FileAccess: FileAccess enumerators have members: Read, ReadWrite
and Write.

• FileShare: FileShare enumerators have the following members:

•Read: It allows opening the file for reading

•ReadWrite: It allows opening the file for reading and writing

•Write: It allows opening the file for writing


Cont..
Writing a file
• The c# System.IO.File class Library Provides methods that enable a
program to easily write text into a file on computer.

• The File.WriteAllText() method simply requires two arguments to


specify the text file path and the string to write.

• File.Exists( ) method ensures specified text file does not already exis

• This will return true when file is found ,otherwise it will return false
Cont..
Cont..
• Press F5 to run the application and see a text file written on Your
desktop or see an error.
Reading text and lines
• The C# System.IO.File class library provides a ReadAllText() method
that can be used to read text from an existing file and assign its entire
contents to a string variable.

• Additionally, the System.IO.File class has a ReadAllLines() method, which


can be used to assign each line of a text file to an individual element of a
string array.
Reading text
Cont.…
• Output:
Reading Lines
Reading Assignment
• Appending to a file
• StreamReader
• StreamWritter

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy