📜  C#-文件I / O

📅  最后修改于: 2020-12-28 05:11:53             🧑  作者: Mango


文件是具有特定名称和目录路径的存储在磁盘中的数据的集合。打开文件进行读取或写入时,它成为

流基本上是通过通信路径的字节序列。主要有两个流:输入流输出流输入流用于从文件读取数据(读取操作),输出流用于写入文件(写入操作)。

C#I / O类

System.IO命名空间具有各种类,这些类用于对文件执行大量操作,例如创建和删除文件,从文件读取或写入文件,关闭文件等。

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

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.

FileStream类

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

您需要创建一个FileStream对象来创建一个新文件或打开一个现有文件。创建FileStream对象的语法如下-

FileStream  = new FileStream( , ,
   , );

例如,我们创建一个FileStream对象F来读取名为sample.txt的文件,如下所示

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

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

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

编译并执行上述代码后,将产生以下结果-

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

C#中的高级文件操作

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

Sr.No. Topic & Description
1 Reading from and Writing into Text files

It involves reading from and writing into text files. The StreamReader and StreamWriter class helps to accomplish it.

2 Reading from and Writing into Binary files

It involves reading from and writing into binary files. The BinaryReader and BinaryWriter class helps to accomplish this.

3 Manipulating the Windows file system

It gives a C# programamer the ability to browse and locate Windows files and directories.