以字节形式向磁盘写入数据通常称为字节流(比特流)

常常使用System.Io

常用的类

说明
File
提供用于创建、复制、删除、移动和打开文件的静态方法,并协助创建 FileStream 对象。
FileInfo
提供创建、复制、删除、移动和打开文件的实例方法,并且帮助创建 FileStream 对象。无法继承此类。
FileStream
公开以文件为主的 Stream,既支持同步读写操作,也支持异步读写操作。
BinaryReader
用特定的编码将基元数据类型读作二进制值。
BinaryWriter
以二进制形式将基元类型写入流,并支持用特定的编码写入字符串。
BufferedStream
给另一流上的读写操作添加一个缓冲层。无法继承此类。
Directory
公开用于创建、移动和枚举通过目录和子目录的静态方法。无法继承此类。
DirectoryInfo
公开用于创建、移动和枚举目录和子目录的实例方法。无法继承此类。
Path
对包含文件或目录路径信息的 String 实例执行操作。这些操作是以跨平台的方式执行的。
StreamReader
实现一个 TextReader,使其以一种特定的编码从字节流中读取字符。
StreamWriter
实现一个 TextWriter,使其以一种特定的编码向流中写入字符。
FileSysWatcher
侦听文件系统更改通知,并在目录或目录中的文件发生更改时引发事件。

基本使用类的介绍

1.1File

常用方法

File提供用于创建、复制、删除、移动和打开文件的静态方法

方法
说明
Move
将指定文件移到新位置,并提供指定新文件名的选项。
Delete
删除指定的文件。如果指定的文件不存在,则不引发异常。
Copy
已重载。 将现有文件复制到新文件。
CreateText
创建或打开一个文件用于写入 UTF-8 编码的文本。
OpenText
打开现有 UTF-8 编码文本文件以进行读取。
Open
已重载。 打开指定路径上的 FileStream。

 

eg 

 1 using System;
 2 using System.IO;
 3 class Test
 4 {
 5     public static void Main()
 6     {
 7         string path = @"c:\temp\123.txt";
 8         if (!File.Exists(path))
 9         {
10             // 创建文件以便写入内容。
11             using (StreamWriter sw = File.CreateText(path))
12             {
13                 sw.WriteLine("Hello");
14                 sw.WriteLine("And");
15                 sw.WriteLine("Welcome");
16             }
17         }
18         // 打开文件从里面读数据。
19         using (StreamReader sr = File.OpenText(path))
20         {
21             string s = "";
22             while ((s = sr.ReadLine()) != null)
23             {
24                 Console.WriteLine(s);
25             }
26         }
27         try
28         {
29             string path2 = path + "temp";
30             // 确认将要拷贝成的文件是否已经有同名的文件存在。
31             File.Delete(path2);
32             // 拷贝文件。
33             File.Copy(path, path2);
34             Console.WriteLine("{0} was copied to {1}.", path, path2);
35             // 删除新生成的文件。
36             File.Delete(path2);
37             Console.WriteLine("{0} was successfully deleted.", path2);
38         }
39         catch (Exception e)
40         {
41             Console.WriteLine("The process failed: {0}", e.ToString());
42         }
43     }
44 }
File 使用

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
  • 2021-07-23
  • 2021-12-13
  • 2021-05-05
  • 2021-08-27
  • 2021-11-17
猜你喜欢
  • 2022-12-23
  • 2021-11-24
  • 2021-08-21
  • 2021-05-18
  • 2022-12-23
  • 2021-08-27
  • 2022-01-18
相关资源
相似解决方案