Dev

Sunday, 10 January 2016

File Handling

File Handling concept in C#.

File handling simply deal with creating, reading, deleting and editing files. Website can handle local files. It can read from, and write to, a text file on the disk. The code can run on any server with file system privileges—and also a local development machine
There are some methods of File which is mostly used in Real application.
1.    Create :-In this mode New File will be created .If  File is Exist then It will be Overridden.
How to create a File using C# File class ?
In order to create a new File using C# File class , we can call Create method in the File class.
CSharp Code : File.Create("c:\\testFile.txt");

2.     Append :-  In Append mode Existing File will be open and contents will be written from Last .If File  is not Exist then new File will be created. Append mode is used Between two Files.

3.    Create New:-In this mode New File will be created, if File is already Exist then it will thrown Exception.

4.    Open:- In this Existing file will be opened if file  is not Exist then it will be thrown Exception. 

5.    OpenRead:- In this Opens the Existing File for Reading the Contents.

6.    Truncate:-In this  Existing File will be open and the Entire contents of the file will be removed.But if the file is not Exist then it will be thrown Exception.
 StreamReader Class:
§  Flush():- Flush function is used for immediately save the File contents from Buffer to Memory.
§  Close():-Close function is used for closing the file.If we do not write close() statement in our program then File will be always open mode ,then No other person will be used this File at that time. This is the reason for using close statement in  the File Program.
§  Read()- It is used for Reading the Value using File Stream.
§  Read-line():-It is used  for Read  the value  using File Stream in a File Line by Line.
§  Peek():- It returns next value but not use it.
§  Seek():- It is used for Read/Write a values at any positions in a file.
StreamWriter Class:
§  close()-->Close function is used for closing the file.
§  Flush():-Flush function is used for immediately save the File contents from Buffer to Memory.
§  Write():- It is used for writing a File using File stream class.


§  WriteLine():- It is used to write a File Line by Line using File stream.

No comments:

Post a Comment