📜  C#中的File.OpenWrite()方法与示例

📅  最后修改于: 2021-05-29 15:03:57             🧑  作者: Mango

File.OpenWrite(String)是一个内置的File类方法,该方法用于打开现有文件或创建要写入的新文件。

句法:

public static System.IO.FileStream OpenWrite (string path);

参数:该函数接受如下所示的参数:

  • 路径:这是将要打开以进行写入的指定文本文件。

例外情况:

  • UnauthorizedAccessException:调用者没有所需的权限。或路径指定了只读文件或目录。
  • ArgumentException:路径是长度为零的字符串,仅包含空格,或者由InvalidPathChars定义的一个或多个无效字符。
  • ArgumentNullException:路径为null。
  • PathTooLongException:指定的路径,文件名或两者都超过系统定义的最大长度。
  • DirectoryNotFoundException:指定的路径无效。
  • NotSupportedException:路径格式无效。

返回值:在具有写访问权限的指定路径上返回未共享的FileStream对象。
下面是说明File.OpenWrite(String)方法的程序
程序1:最初没有创建文件。在代码下面,它自己创建一个文件gfg.txt ,向其中写入一些文本,然后进行打印。

C#
// C# program to illustrate the usage
// of File.OpenWrite(String) method
  
// Using System, System.IO and
// System.Text namespaces
using System;
using System.IO;
using System.Text;
  
class GFG {
    public static void Main()
    {
        // Creating a file
        string path = @"gfg.txt";
  
        // Opening the file for writing
        using(FileStream fs = File.OpenWrite(path))
        {
            // Putting below specified contents
            Byte[] info = new UTF8Encoding(true).GetBytes("GFG is a computer science portal.");
            fs.Write(info, 0, info.Length);
        }
  
        // Opening the file for reading
        using(FileStream fs = File.OpenRead(path))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
  
            while (fs.Read(b, 0, b.Length) > 0) {
                // Printing the contents of the file
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
}


CSharp
// C# program to illustrate the usage
// of File.OpenWrite(String) method
  
// Using System, System.IO and
// System.Text namespaces
using System;
using System.IO;
using System.Text;
  
class GFG {
    public static void Main()
    {
        // Specifying a file
        string path = @"file.txt";
  
        // Opening the file for writing
        using(FileStream fs = File.OpenWrite(path))
        {
            // Overwriting the above file with below
            // specified contents
            Byte[] info = new UTF8Encoding(true).GetBytes("GFG is a CS portal.");
            fs.Write(info, 0, info.Length);
        }
  
        // Opening the file for reading
        using(FileStream fs = File.OpenRead(path))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
  
            while (fs.Read(b, 0, b.Length) > 0) {
                // Printing the final contents of the file
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
}


输出:

GFG is a computer science portal.

上面的代码给出了上面的输出,并创建了一个文件,如下所示:

gfg.txt

程序2:最初,将创建一个文件file.txt ,其内容如下所示-

file.txt

下面的代码将用其他指定的内容覆盖文件内容,然后将打印最终内容。

尖锐的

// C# program to illustrate the usage
// of File.OpenWrite(String) method
  
// Using System, System.IO and
// System.Text namespaces
using System;
using System.IO;
using System.Text;
  
class GFG {
    public static void Main()
    {
        // Specifying a file
        string path = @"file.txt";
  
        // Opening the file for writing
        using(FileStream fs = File.OpenWrite(path))
        {
            // Overwriting the above file with below
            // specified contents
            Byte[] info = new UTF8Encoding(true).GetBytes("GFG is a CS portal.");
            fs.Write(info, 0, info.Length);
        }
  
        // Opening the file for reading
        using(FileStream fs = File.OpenRead(path))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
  
            while (fs.Read(b, 0, b.Length) > 0) {
                // Printing the final contents of the file
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
}

输出:

GFG is a CS portal.