Csharp 简明教程

C

file 是存储在具有特定名称和目录路径的磁盘中的数据集合。当一个文件被打开用于读取或写入时,它将变为 stream

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

流基本上是通过通信路径传递的一系列字节。有两个主流: input streamoutput stream输入流 用于从文件中读取数据(读取操作), output stream 用于将数据写入文件(写入操作)。

The stream is basically the sequence of bytes passing through the communication path. There are two main streams: the input stream and the output stream. The* input stream* is used for reading data from file (read operation) and the output stream is used for writing into the file (write operation).

C

System.IO 命名空间具有用于执行众多文件操作的各种类,例如创建和删除文件、从文件读取或向文件写入、关闭文件等。

The System.IO namespace has various classes that are used for performing numerous operations with files, such as creating and deleting files, reading from or writing to a file, closing a file etc.

下表显示了 System.IO 命名空间中使用的一些常用非抽象类:

The following table shows some commonly used non-abstract classes in the System.IO namespace −

Sr.No.

I/O Class & Description

1

BinaryReader Reads primitive data from a binary stream.

2

BinaryWriter Writes primitive data in binary format.

3

BufferedStream A temporary storage for a stream of bytes.

4

Directory Helps in manipulating a directory structure.

5

DirectoryInfo Used for performing operations on directories.

6

DriveInfo Provides information for the drives.

7

File Helps in manipulating files.

8

FileInfo Used for performing operations on files.

9

FileStream Used to read from and write to any location in a file.

10

MemoryStream Used for random access to streamed data stored in memory.

11

Path Performs operations on path information.

12

StreamReader Used for reading characters from a byte stream.

13

StreamWriter Is used for writing characters to a stream.

14

StringReader Is used for reading from a string buffer.

15

StringWriter Is used for writing into a string buffer.

The FileStream Class

System.IO 命名空间中的 FileStream 类有助于读取、写入和关闭文件。此类派生自抽象类 Stream。

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 对象才能创建新文件或打开现有文件。创建 FileStream 对象的语法如下 −

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>);

例如,我们为读取名为 sample.txt as shown 的文件创建一个 F FileStream 对象 −

For example, we create a FileStream object F for reading a file named sample.txt as shown

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

Sr.No.

Parameter & Description

1

FileMode The FileMode enumerator defines various methods for opening files. The members of the FileMode enumerator are − Append − It opens an existing file and puts cursor at the end of file, or creates the file, if the file does not exist. Create − It creates a new file. CreateNew − It specifies to the operating system, that it should create a new file. Open − It opens an existing file. OpenOrCreate − It specifies to the operating system that it should open a file if it exists, otherwise it should create a new file. Truncate − It opens an existing file and truncates its size to zero bytes.

2

FileAccess FileAccess enumerators have members: Read, ReadWrite and Write.

3

FileShare FileShare enumerators have the following members − Inheritable − It allows a file handle to pass inheritance to the child processes None − It declines sharing of the current file Read − It allows opening the file for readin. ReadWrite − It allows opening the file for reading and writing Write − It allows opening the file for writing

Example

以下程序演示了 *FileStream *类的用法 −

The following program demonstrates 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 result −

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

上述示例为 C# 中简单文件操作提供了示例。但是,为了利用 C# System.IO 类的强大功能,您需要了解这些类的常用属性和方法。

The preceding example provides simple file operations in C#. However, to utilize the immense powers of C# System.IO classes, you need to know the commonly used properties and methods of these classes.

Sr.No.

Topic & Description

1

Reading from and Writing into Text filesIt involves reading from and writing into text files. The StreamReader and StreamWriter class helps to accomplish it.

2

Reading from and Writing into Binary filesIt involves reading from and writing into binary files. The BinaryReader and BinaryWriter class helps to accomplish this.

3

Manipulating the Windows file systemIt gives a C# programamer the ability to browse and locate Windows files and directories.