I / O file in C #
A file is a collection of data stored on the drive with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream.
A file is a collection of data stored on the drive with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream .
Basically, stream is a sequence of bytes passed through the path. There are two important streams : Input stream and Output stream . Input stream is used to read data from file (read action) and Output stream is used to write to file (write action).
I / O classes in C #
System.IO Namespace has many different layers, used to perform file operations, such as creating and deleting files, reading and writing a file, closing a file .
The following table shows some commonly used non-abstract classes in the System.IO namespace in C #:
I / O Class Description BinaryReader Read primitive data from a binary stream. BinaryWriter Write the original data in binary format. BufferedStream Temporary memory for the bytes of a stream. Directory Help control directory structure. DirectoryInfo Used to perform operations on directories. DriveInfo Provides information for drives. File Helps in manipulating files. FileInfo Used to perform operations on files. FileStream Use to read and write any location in a file. MemoryStream Used for random access to streams stored in memory. Path Performs operations on path information. StreamReader Used to read characters from a stream of bytes. StreamWriter Used to write characters to a stream. StringReader Used to read from a string buffer. StringWriter Used to write to a string buffer.FileStream class in C #
FileStream class in the System.IO namespace in C # helps read, write, and close 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 in C # is as follows:
FileStream < t ê n_ đố i_t ượ ng > = new FileStream( < t ê n_file > , ,
, );
For example : Create a FileStream object F to read a file with the name vidu.txt, as follows:
FileStream F = new FileStream(" vidu.txt ", FileMode.Open, FileAccess.Read,ParameterDescriptionFileMode
FileShare.Read);
The FileMode enumerator defines different methods to open the file. The members of the FileMode enumerator are:
-
Append : Open an existing file and place the cursor at the end of the file, or create a File, if the file doesn't already exist.
-
Create : Create a new file.
-
CreateNew : Determine with the operating system that it will create a new file.
-
Open : Open an existing file.
-
OpenOrCreate : Determine with the operating system that it will open a file if it exists, otherwise it will create a new file.
-
Truncate : Open an existing file and truncate its size to 0 bytes.
FileAccess enumerators have members: Read , ReadWrite and Write .
FileShareFileShare enumerators have the following members:
-
Inheritable : Allows a traditional file to inherit to child processes.
-
None : Reject the current file sharing.
-
Read : Allow to open the file to read.
-
ReadWrite : Allows opening files to read and write.
-
Write : Allows you to open files for recording.
For example:
Here is an example to illustrate how to use the FileStream class in C #:
using System ; using System . IO ; namespace VdIO { class Program { static void Main ( string [] args ) { FileStream F = new FileStream ( "test.dat" , FileMode . OpenOrCreate , FileAccess . ReadWrite ); for ( int i = 1 ; i <= 20 ; i ++) { F . WriteByte (( byte ) i ); } F . Position = 0 ; for ( int i = 0 ; i <= 20 ; i ++) { Console . Write ( F . ReadByte () + " " ); } F . Close (); Console . ReadKey (); } } }
Use the Console.ReadKey command (); to see the results more clearly. Compiling and running the above C # program will produce the following results:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1
Advanced file operations in C #
The previous example illustrates simple operations on files in C #. However, to take full advantage of the System.IO classes in C #, you need to know the commonly used properties and methods for these classes.
- Reading and writing text files: StreamReader and StreamWriter classes help to read and write text files.
- Read and write binary files: The BinaryReader and BinaryWriter classes help to read and write binary files.
- Manipulating Windows file system: It provides C # programmers the ability to browse and locate files and folders in Windows.
Follow tutorialspoint
Previous article: Handling exceptions (Try / Catch / Finally) in C #
Next lesson: Attribute in C #
You should read it
- Pseudo-Class in CSS
- Storage class in C / C ++
- The service takes super fast torrent files using FileStream
- Class selector in CSS
- Class (Class) in C #
- How to Crash a Class in College
- Storage class in C programming
- Interface in C ++ (Abstract class)
- Learn Class and Object in Python
- How To Make The Best Class Assignments?
- How to Write a Class in Python
- Contructor and Destructor in C ++
Maybe you are interested
NASA's space telescope finds three new planets with rare features Not any organization, but these families are quietly controlling the world Maintaining these 9 habits, you don't want to get old Looking at the earwax, you will know right away if you are susceptible to armpit odor. Discover human limits through 5 senses Top 10 most special people in the world