FILE Input Output

Posted on

FILE IO IN OPERATIONS IN C#

A record is a gathering of information put away in a plate with a particular name and a catalog way. At the point when a document is opened for perusing or composing, it turns into a stream. The stream is fundamentally the grouping of bytes going through the correspondence way.

There are two standards:

The information stream and the yield stream. The information stream is utilized for perusing information from document (read operation) and the yield stream is utilized for composing into the record (compose operation).

The Directory(Info) and File(Info) Types

System.IO gives four classes that permit you to control singular documents, and in addition communicate with a machine’s catalog structure. The initial two sorts, Directory and File, uncover creation, erasure, replicating, and moving operations utilizing different static individuals. The firmly related FileInfo and DirectoryInfo sorts uncover comparative usefulness as example level strategies (along these lines, you should allot them with the new watchword). In Figure 20-1, the Directory and File classes straightforwardly amplify System.Object, while DirectoryInfo and FileInfo get from the unique FileSystemInfo sort.

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.

FileStream <object_name> = new FileStream( <file_name>, <FileMode Enumerator>, <FileAccess Enumerator>, <FileShare Enumerator>);

For instance, we make a FileStream object F for perusing a record named sample.txt as appeared:

FileStream F = new FileStream(“sample.txt”, FileMode.Open, FileAccess.Read, FileShare.Read);

Parameter Description FileMode

The FileMode enumerator characterizes different strategies for opening documents.

The individuals from the FileMode enumerator are:

Annex: It opens a current document and puts cursor toward the end of record, or makes the record, if the record does not exist.

Make: It makes another document.

CreateNew: It determines to the working framework, that it ought to make another record.

Open: It opens a current document.

OpenOrCreate: It determines to the working framework that it ought to open a document on the off chance that it exists, else it ought to make another record.

Truncate: It opens a current document and truncates its size to zero bytes.

FileAccess

FileAccess enumerators have individuals: Read, ReadWrite and Write.

FileShare

FileShare enumerators have the accompanying individuals:

Inheritable: It permits a document handle to pass legacy to the tyke forms

Perused: It permits opening the record for perusing.

ReadWrite: It permits opening the record for perusing and composing.

Keep in touch with: It permits opening the record for composing.

Advanced File Operations in C#

C# – Reading from and Writing to Text Files

The StreamReader and StreamWriter classes are utilized for perusing from and composing information to content documents. These classes acquire from the conceptual base class Stream, which underpins perusing and composing bytes into a record stream.

The StreamReader Class

The StreamReader class additionally acquires from the unique base class TextReader that speaks to a peruser for perusing arrangement of characters.

public override int Read()

Reads the next character from the input stream and advances the character position by one.

public override void Close()

It closes the StreamReader object and the underlying stream, and releases any system resources associated with the reader.

public override int Peek()

Returns the next available character but does not consume it.

The StreamWriter Class

The StreamWriter class acquires from the unique class TextWriter that speaks to an essayist, which can compose a progression of character.

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Leave a comment