C# File Handling
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.
•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
5
WHY YOU NEED TO LEARN FILE
HANDLING
• 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
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.
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
13
STREAMREADER CLASS
• 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?
16
WHAT IS TEXTREADER CLASS?
Alejandro B. Sotelo
09666470918
[email protected]
[email protected]