File.CreateText(String) 方法

定义

创建或打开用于写入 UTF-8 编码文本的文件。Creates or opens a file for writing UTF-8 encoded text. 如果该文件已存在,将覆盖其内容。If the file already exists, its contents are overwritten.

public:
 static System::IO::StreamWriter ^ CreateText(System::String ^ path);
public static System.IO.StreamWriter CreateText (string path);
static member CreateText : string -> System.IO.StreamWriter
Public Shared Function CreateText (path As String) As StreamWriter

参数

path
String

要打开以进行写入的文件。The file to be opened for writing.

返回

StreamWriter

一个 StreamWriter,它使用 UTF-8 编码写入到指定的文件。A StreamWriter that writes to the specified file using UTF-8 encoding.

例外

调用方没有所要求的权限。The caller does not have the required permission.

- 或 --or- path 指定了一个只读文件。path specified a file that is read-only.

- 或 --or- path 指定了一个隐藏文件。path specified a file that is hidden.

path 是一个长度为零的字符串,仅包含空格,或包含一个或多个由 InvalidPathChars 定义的无效字符。path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

pathnullpath is null.

指定的路径和/或文件名超过了系统定义的最大长度。The specified path, file name, or both exceed the system-defined maximum length.

指定的路径无效(例如,它位于未映射的驱动器上)。The specified path is invalid (for example, it is on an unmapped drive).

path 的格式无效。path is in an invalid format.

示例

下面的示例创建一个文件,用于写入和读取文本。The following example creates a file for text writing and reading.

using namespace System;
using namespace System::IO;
int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   if ( !File::Exists( path ) )
   {
      
      // Create a file to write to.
      StreamWriter^ sw = File::CreateText( path );
      try
      {
         sw->WriteLine( "Hello" );
         sw->WriteLine( "And" );
         sw->WriteLine( "Welcome" );
      }
      finally
      {
         if ( sw )
                  delete (IDisposable^)sw;
      }
   }
   
   // Open the file to read from.
   StreamReader^ sr = File::OpenText( path );
   try
   {
      String^ s = "";
      while ( s = sr->ReadLine() )
      {
         Console::WriteLine( s );
      }
   }
   finally
   {
      if ( sr )
            delete (IDisposable^)sr;
   }
}
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";
        if (!File.Exists(path))
        {
            // Create a file to write to.
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }	
        }

        // Open the file to read from.
        using (StreamReader sr = File.OpenText(path))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null)
            {
                Console.WriteLine(s);
            }
        }
    }
}
Imports System.IO
Imports System.Text

Public Class Test
  Public Shared Sub Main()
    Dim path As String = "c:\temp\MyTest.txt"

    If Not File.Exists(path) Then
      ' Create a file to write to. 
      Using sw As StreamWriter = File.CreateText(path)
        sw.WriteLine("Hello")
        sw.WriteLine("And")
        sw.WriteLine("Welcome")
      End Using
    End If

    ' Open the file to read from. 
    Using sr As StreamReader = File.OpenText(path)
      Do While sr.Peek() >= 0
        Console.WriteLine(sr.ReadLine())
      Loop
    End Using

  End Sub
End Class

注解

此方法等效于 StreamWriter(String, Boolean) 参数设置为的构造函数重载 append falseThis method is equivalent to the StreamWriter(String, Boolean) constructor overload with the append parameter set to false. 如果指定的文件 path 不存在,则创建该文件。If the file specified by path does not exist, it is created. 如果文件存在,则将覆盖其内容。If the file does exist, its contents are overwritten. 允许其他线程在文件打开时读取该文件。Additional threads are permitted to read the file while it is open.

path允许参数指定相对或绝对路径信息。The path parameter is permitted to specify relative or absolute path information. 相对路径信息被解释为相对于当前工作目录。Relative path information is interpreted as relative to the current working directory. 若要获取当前工作目录,请参见 GetCurrentDirectoryTo obtain the current working directory, see GetCurrentDirectory.

有关常见 i/o 任务的列表,请参阅 常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

适用于

另请参阅