Input and output of the C # Tutorial C # File

Source: Internet
Author: User

Input and output of C # files

A file is a collection of data stored on disk with the specified name and directory path. When the file is opened for reading and writing, it becomes a stream.

Essentially, a stream is a sequence of bytes passed through a communication path. There are two main streams: the input stream and the output stream. The input stream is used to read data from a file (read operations), and the output stream is used to write data to the file (write operations).

C # I/O classes

The System.IO namespace has a variety of different classes for performing various file operations, such as creating and deleting files, reading or writing files, closing files, and so on.

The following table lists some of the non-abstract classes commonly used in System.IO namespaces:

I/O class

Describe

BinaryReader reads raw data from a binary stream.

BinaryWriter writes raw data in binary format.

Temporary storage for the BufferedStream byte stream.

Directory helps you manipulate the catalog structure.

DirectoryInfo is used to perform operations on the directory.

DriveInfo provides information about the drive.

File is useful for working with files.

FileInfo is used to perform operations on files.

FileStream is used to read and write anywhere in the file.

MemoryStream is used to randomly access data streams stored in memory.

Path performs an operation on the route information.

The StreamReader is used to read characters from a byte stream.

The StreamWriter is used to write characters to a stream.

The StringReader is used to read a string buffer.

The StringWriter is used to write a string buffer.

FileStream class

The FileStream class in the System.IO namespace helps to read and write files and close them. The 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 is as follows:

FileStream <object_name> = new FileStream (<file_name>,<filemode enumerator>, <fileaccess Enumerator>, <fileshare enumerator>);

For example, create a FileStream object F to read the file named Sample.txt:

FileStream F = new FileStream ("Sample.txt", FileMode.Open, FileAccess.Read, FileShare.Read);

Parameters

Describe

FileMode

The FileMode enumeration defines the various ways to open a file. The members of the FileMode enumeration are:

Append: Open an existing file and place the cursor at the end of the file. If the file does not exist, the file is created.

Create: Creates a new file. If the file already exists, delete the old file, and then create a new file.

CreateNew: Specifies that the operating system should create a new file. Throws an exception if the file already exists.

Open: Opens an existing file. Throws an exception if the file does not exist.

OpenOrCreate: Specifies that the operating system should open an existing file. If the file does not exist, create a new file with the specified name to open it.

Truncate: Opens an existing file that, once opened, will be truncated to a size of 0 bytes. Then we can write new data to the file, but keep the initial creation date of the file. Throws an exception if the file does not exist.

FileAccess

The members of the FileAccess enumeration are: Read, ReadWrite, and Write.

FileShare

The members of the FileShare enumeration are:

Inheritable: Allows file handles to be inherited by child processes. Win32 does not directly support this feature.

None: Decline to share the current file. Any request to open the file (a request made by this process or another process) will fail until the file is closed.

READ: Allows file reads to be opened later. If this flag is not specified, any request to open the file for reading (a request made by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions may be required to access the file.

ReadWrite: Allows subsequent open file reads or writes. If this flag is not specified, any request to open the file for reading or writing (emitted by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions may be required to access the file.

Write: Allows subsequent open file writes. If this flag is not specified, any requests that open the file for writing (requests made by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions may be required to access the file.

Delete: Allows files to be deleted later.

Instance

The following program demonstrates the use of the FileStream class:

Using system;using system.io;namespace fileioapplication{    class program    {        static void Main (string[] args)        {            FileStream F = new FileStream ("Test.dat",             FileMode.OpenOrCreate, fileaccess.readwrite);            for (int i = 1; i <=; i++)            {                f.writebyte ((byte) i);            }            f.position = 0;            for (int i = 0; I <=; i++)            {                Console.Write (f.readbyte () + "");            }            F.close ();            Console.readkey ();}}}    

When the above code is compiled and executed, it produces the following results:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20-1

C # Advanced file operations

The above example shows a simple file operation in C #. However, to take full advantage of the powerful features of the C # System.IO class, you need to know the properties and methods commonly used by these classes.

In the following sections, we will discuss these classes and the actions they perform. Please click on the link to learn more about each section:

Theme

Describe

Text file read and write it involves the text file read and write. The StreamReader and StreamWriter classes help to read and write text files.

Read and write binary files it involves the reading and writing of binary files. The BinaryReader and BinaryWriter classes help to complete the reading and writing of binary files.

Operation of the Windows file system it allows C # programmers to browse and locate Windows files and directories.

The above is the "C # tutorial" C # file input and output content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.