0% found this document useful (0 votes)
110 views18 pages

C# File Handling

The document discusses file handling in C# and how to read and write files using classes like FileStream, StreamWriter, StreamReader and TextWriter. It explains what files and streams are, why file handling is needed, and provides examples of how to open, read and write to files in C# using these classes.

Uploaded by

Alejandro Sotelo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views18 pages

C# File Handling

The document discusses file handling in C# and how to read and write files using classes like FileStream, StreamWriter, StreamReader and TextWriter. It explains what files and streams are, why file handling is needed, and provides examples of how to open, read and write to files in C# using these classes.

Uploaded by

Alejandro Sotelo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

C# FILE HANDLING

IPT
To save the information permanently on the
disk or reading information from the saved file
AGENDA through C# is known as File Handling. C#
File Handling uses a stream to save or retrieve
information.

•What is File Handling in C#?

•Why I need to learn File Handling?

•What is Files and Stream?

•Example
FI LE AND STRE AM

4
A file is a collection of data stored on a disk
with specific name, extension and directory
path. When you open File using C# for
reading and writing purpose it
becomes Stream.
FILE

A stream is a sequence of bytes traveling


from a source to a destination over a STREAM
communication path.

5
WHY YOU NEED TO LEARN FILE
HANDLING

• Several times you need to save information on a disk.

• You will not get database to save information

• Your project may require saving information in txt file, doc file, xls file,
pdf files or any other file types.

6
S Y S T E M . T E X T N A M E S PA C E

C O N TA I N S C L A S S E S T H AT R E P R E S E N T A S C I I A N D
UNICODE CHARACTER ENCODING , ABSTRACT BASE
C L A S S E S F O R C O N V E RT I N G B L O C K S O F C H A R A C T E R T O
A N D F R O M B L O C K S O F B Y T E S A N D H E L P M A N I P U L AT E S
A N D F O R M AT S T R I N G O B J E C T
S Y S T E M . I O N A M E S PA C E
A C O L L E C T I O N O F C L A S S E S , M E T H O D S , E N U M E R AT I O N
A N D T Y P E S T H AT I S R E S P O N S I B L E F O R R E A D I N G ,
WRITING AND ACCESSING OF FILES.

FILESTREAM CLASS
U S E D T O P E R F O R M T H E B A S I C O P E R AT I O N O R R E A D I N G
A N D W R I T I N G O P E R AT I N G S Y S T E M F I L E . F I L E S T R E A M
CLASS HELPS IN READING FROM, WRITING AND
CLOSING FILES.
FILESTREAM CLASS
S Y N TA X

FileStream <object_name> = new FileStream( <file_name>,


<FileMode Enumerator>, <FileAccess Enumerator>,
<FileShare Enumerator>);
FILEMODE
Specifies how the operation system should open the file

APPEND - CREATENEW -
• Open the file if exist or create a • It create a new file and if file already
new file. If file exists then place exist then throw IOException
cursor at the end of the file
OPEN -
CREATE-
• Open existing file
• It specifies the operating system to
OPEN OR CREATE –
create a new file, if file already
exists then previous file will be • Open existing file and if the not found
overwritten then create new file
TRUNCATE –
• Open an existing file and cut all the
10
stored data. File size becomes 0.
FILEACCESS
It gives permission to file whether it will open Read,
ReadWrite or Write
DELETE- READWRITE -
• Allows subsequent deleting of a file • It allows subsequent opening of the file
INHERITABLE - for reading and writing.

• It passes inheritance to child process WRITE -

NONE - • Allows subsequent opening of the file


for writing.
• It declines sharing of the current files

READ -
• It allows subsequent opening of the file
for reading.

11
STREAMWRITER CLASS

Class is more popular in File Handling, and it is very helpful writing text data in the file
• @ symbol before a var is used if you want to call a variable class (var @class = 1; , var @virtual = 2;,
var @return = 3;)
FOR STRING IT USES THE LITERAL, FOR EXAMPLE
“c:\\test\\example\\”
is equivalent to
@”C:\test\example\”

12
HOW TO WRITE TEXT INTO A FILE USING
STREAMWRITER CLASS? WHAT IS
STREAMWRITER
CLASS?
DOCUMENTS
ATTACH

EXERCISES 1 AND 2 StreamWriter Class is more popular in


File Handling, and it is very helpful in
writing text data in the file is easy to
use and provides a complete set of
constructors and methods to work

13
STREAMREADER CLASS

Allows you to read text files easily


• Implements a TextReader that read characters from a byte stream in a particular encoding
• StreamReader class uses UTF-8 Encoding by defaults
• StreamReader class is designed for character input in a particular encoding
• Use this class for reading standard text file

• UTF-8 is a Unicode encoding that represents each code point as a sequence of one to four
bytes. Unlike the UTF-16 and UTF-32 encodings,

14
HOW TO READ
FILE USING
STREAMREADER
CLASS?

DOCUMENTS
ATTACH

EXERCISES 3
WHAT IS TEXTWRITER CLASS?

The TextWriter class represents a writer


that can write sequential series of
characters. You can use this class to write Documents attach
text in a file. It is an abstract base class Exercise 4
of StreamWriter and StringWriter, which
write characters to streams and string
respectively.

16
WHAT IS TEXTREADER CLASS?

DOCUMENTS ATTACH EXERCISES 5


17
THANK YOU

Alejandro B. Sotelo​
09666470918
[email protected]
[email protected]

You might also like