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

📅  最后修改于: 2021-05-29 19:44:02             🧑  作者: Mango

File.OpenText(String)是一个内置的File类方法,用于打开现有的UTF-8编码文本文件进行读取。
句法:

public static System.IO.StreamReader OpenText (string path);

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

例外情况:

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

返回值:返回指定路径上的StreamReader。
下面是说明File.OpenText(String)方法的程序。
程序1:在运行以下代码之前,将创建一个文本文件file.txt ,其内容如下所示-

file.txt

在下面的代码中,打开文本文件file.txt以进行读取。

C#
// C# program to illustrate the usage
// of File.OpenText(String) method
  
// Using System and System.IO
// namespaces
using System;
using System.IO;
  
class Test {
    public static void Main()
    {
        // Specifying a text file
        string path = @"file.txt";
  
        // Opening the file for reading
        using(StreamReader sr = File.OpenText(path))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) {
                // printing the file contents
                Console.WriteLine(s);
            }
        }
    }
}


C#
// C# program to illustrate the usage
// of File.OpenText(String) method
  
// Using System and System.IO
// namespaces
using System;
using System.IO;
  
class Test {
    public static void Main()
    {
        // Specifying a text file
        string path = @"file.txt";
  
        // Checking the existance of file
        if (File.Exists(path)) {
            using(StreamWriter sw = File.CreateText(path))
            {
                // Overwriting the file with below
                // specified contents
                sw.WriteLine("GFG is a CS portal.");
            }
        }
  
        // Opening the file for reading
        using(StreamReader sr = File.OpenText(path))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) {
                // printing the overwritten content
                Console.WriteLine(s);
            }
        }
    }
}


执行中:

GeeksforGeeks

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

file.txt

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

C#

// C# program to illustrate the usage
// of File.OpenText(String) method
  
// Using System and System.IO
// namespaces
using System;
using System.IO;
  
class Test {
    public static void Main()
    {
        // Specifying a text file
        string path = @"file.txt";
  
        // Checking the existance of file
        if (File.Exists(path)) {
            using(StreamWriter sw = File.CreateText(path))
            {
                // Overwriting the file with below
                // specified contents
                sw.WriteLine("GFG is a CS portal.");
            }
        }
  
        // Opening the file for reading
        using(StreamReader sr = File.OpenText(path))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) {
                // printing the overwritten content
                Console.WriteLine(s);
            }
        }
    }
}

执行中:

GFG is a CS portal.