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 .
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
May be interested
- What file is M4A? How to open, edit and convert M4A filesthe file is part of an extension .m4a is an mpeg-4 audio file. they are usually found in apple's itunes store and are considered as the default format of songs when downloaded.
- What file is XSD? How to open, edit and convert XSD filesa file with the .xsd extension is most likely an xml schema file. this is a text-based file format, which defines the validation rules for xml files and explains xml forms.
- What are IGS files? How to open the IGS . filea file with the igs extension is most likely an iges drawing file used by cad programs to save vector image data in the ascii text format.
- What is CHD file? How to use CHD file?if you're playing retro games, you may have come across files with the extension '.chd'. let tipsmake.com learn about chd file and how to use it through the following article!
- The Fsutil file in Windowsthe fsutil file finds a file by the user name (if disk quotas is enabled), the query allocates the scope to a file, sets the short name to a file, sets the valid data length of the file, sets the data zero for a file, or create a new file.
- Instructions for creating and using BAT file on Windowshow to create a simple batch file and some basics about it will be presented in this article. at the same time you will be provided with some resources for learning and writing batch files.
- How to create an EXE installation filesometimes to install certain software on your computer, you must use the .exe file, but have you ever wondered what the purpose of this file is and how it was created? in this article, tipsmake.com will answer that question and guide you to create an exe installation file.
- File & I / O in PHPin this article we will learn about explaining file-related functions in php.
- What kind of file is M3U8 file?a file with a .m3u8 extension is a file that contains utf-8 encoded audio playlists
- What is an exe file?a file with the exe extension is an executable file used in operating systems such as windows, ms-dos, openvms, and reactos to open software programs.