File.CreateText(String) 方法

定義

建立或開啟用以寫入 UTF-8 編碼文字的檔案。 如果檔案已經存在,則會取代其內容。

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

要被開啟來寫入的檔案。

傳回

StreamWriter,使用 UTF-8 編碼方式寫入指定檔案。

例外狀況

呼叫端沒有必要的權限。

-或-

path 指定了唯讀的檔案。

-或-

path 指定了隱藏的檔案。

.NET Framework和 2.1 之前的 .NET Core 版本: path 是長度為零的字串、只包含空白字元,或包含一或多個無效字元。 您可以使用 GetInvalidPathChars() 方法查詢無效字元。

pathnull

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

指定的路徑無效 (例如,它位於未對應的磁碟機上)。

path 格式無效。

範例

下列範例會建立用於文字寫入和讀取的檔案。

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);
            }
        }
    }
}
open System.IO

let path = @"c:\temp\MyTest.txt"

if File.Exists path |> not then
    // Create a file to write to.
    use sw = File.CreateText path
    sw.WriteLine "Hello"
    sw.WriteLine "Welcome"

// Open the file to read from.
do
    use sr = File.OpenText path
    let mutable s = sr.ReadLine()

    while isNull s |> not do
        printfn $"{s}"
        s <- sr.ReadLine()
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 參數設定為 false 。 如果 指定的 path 檔案不存在,則會建立它。 如果檔案存在,則會取代其內容。 開啟檔案時,允許讀取其他執行緒。

允許 path 參數指定相對或絕對路徑資訊。 相對路徑資訊會解譯為相對於目前工作目錄。 若要取得目前的工作目錄,請參閱 GetCurrentDirectory

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

適用於

另請參閱