Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The input and output of the C# file


May 11, 2021 C#


Table of contents


The input and output of the C# file

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

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

Class C#I/O

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

The following table lists some of System.IO classes commonly used in the namespace:

I/O 类 描述
BinaryReader 从二进制流读取原始数据。
BinaryWriter 以二进制格式写入原始数据。
BufferedStream 字节流的临时存储。
Directory 有助于操作目录结构。
DirectoryInfo 用于对目录执行操作。
DriveInfo 提供驱动器的信息。
File 有助于处理文件。
FileInfo 用于对文件执行操作。
FileStream 用于文件中任何位置的读写。
MemoryStream 用于随机访问存储在内存中的数据流。
Path 对路径信息执行操作。
StreamReader 用于从字节流中读取字符。
StreamWriter 用于向一个流中写入字符。
StringReader 用于读取字符串缓冲区。
StringWriter 用于写入字符串缓冲区。

FileStream class

System.IO FileStream class in the namespace helps to read and write files and close them. This class is derived 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 a file .txt sample:

FileStream F = new FileStream("sample.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
参数 描述
FileMode

FileMode 枚举定义了各种打开文件的方法。FileMode 枚举的成员有:

  • Append :打开一个已有的文件,并将光标放置在文件的末尾。如果文件不存在,则创建文件。
  • Create :创建一个新的文件。如果文件已存在,则删除旧文件,然后创建新文件。
  • CreateNew :指定操作系统应创建一个新的文件。如果文件已存在,则抛出异常。
  • Open :打开一个已有的文件。如果文件不存在,则抛出异常。
  • OpenOrCreate :指定操作系统应打开一个已有的文件。如果文件不存在,则用指定的名称创建一个新的文件打开。
  • Truncate :打开一个已有的文件,文件一旦打开,就将被截断为零字节大小。然后我们可以向文件写入全新的数据,但是保留文件的初始创建日期。如果文件不存在,则抛出异常。
FileAccess

FileAccess 枚举的成员有: Read ReadWrite Write

FileShare

FileShare 枚举的成员有:

  • Inheritable :允许文件句柄可由子进程继承。Win32 不直接支持此功能。
  • None :谢绝共享当前文件。文件关闭前,打开该文件的任何请求(由此进程或另一进程发出的请求)都将失败。
  • Read :允许随后打开文件读取。如果未指定此标志,则文件关闭前,任何打开该文件以进行读取的请求(由此进程或另一进程发出的请求)都将失败。但是,即使指定了此标志,仍可能需要附加权限才能够访问该文件。
  • ReadWrite :允许随后打开文件读取或写入。如果未指定此标志,则文件关闭前,任何打开该文件以进行读取或写入的请求(由此进程或另一进程发出)都将失败。但是,即使指定了此标志,仍可能需要附加权限才能够访问该文件。
  • Write :允许随后打开文件写入。如果未指定此标志,则文件关闭前,任何打开该文件以进行写入的请求(由此进程或另一进过程发出的请求)都将失败。但是,即使指定了此标志,仍可能需要附加权限才能够访问该文件。
  • Delete :允许随后删除文件。

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 <= 20; i++)
            {
                F.WriteByte((byte)i);
            }
            F.Position = 0;
            for (int i = 0; i <= 20; 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

Advanced file operations for C#

The example above demonstrates the simple file operation in C#. However, to take full advantage of System.IO of classes, you need to know the properties and methods commonly used by those classes.

In the following sections, we'll discuss these classes and what they do. Click the link to learn more about each section:

主题 描述
文本文件的读写 它涉及到文本文件的读写。 StreamReader StreamWriter 类有助于完成文本文件的读写。
二进制文件的读写 它涉及到二进制文件的读写。 BinaryReader BinaryWriter 类有助于完成二进制文件的读写。
Windows 文件系统的操作 它让 C# 程序员能够浏览并定位 Windows 文件和目录。