FileInfo.OpenWrite 方法

定义

创建一个只写的 FileStreamCreates a write-only FileStream.

public:
 System::IO::FileStream ^ OpenWrite();
public System.IO.FileStream OpenWrite ();
member this.OpenWrite : unit -> System.IO.FileStream
Public Function OpenWrite () As FileStream

返回

新的或现有文件的只写非共享的 FileStream 对象。A write-only unshared FileStream object for a new or existing file.

异常

创建 FileInfo 对象的实例时指定的路径为只读或是目录。The path specified when creating an instance of the FileInfo object is read-only or is a directory.

创建 FileInfo 对象的实例时指定的路径无效,例如此路径位于未映射的驱动器。The path specified when creating an instance of the FileInfo object is invalid, such as being on an unmapped drive.

示例

下面的示例打开一个文件以进行写入, 然后从该文件中读取。The following example opens a file for writing and then reads from the file.

using namespace System;
using namespace System::IO;
using namespace System::Text;

int main()
{
   String^ path = "c:\\Temp\\MyTest.txt";
   FileInfo^ fi = gcnew FileInfo( path );

   // Open the stream for writing.
   {
      FileStream^ fs = fi->OpenWrite();
      try
      {
         array<Byte>^info = (gcnew UTF8Encoding( true ))->GetBytes( "This is to test the OpenWrite method." );
         
         // Add some information to the file.
         fs->Write( info, 0, info->Length );
      }
      finally
      {
         if ( fs )
            delete (IDisposable^)fs;
      }
   }
   
   // Open the stream and read it back.
   {
      FileStream^ fs = fi->OpenRead();
      try
      {
         array<Byte>^b = gcnew array<Byte>(1024);
         UTF8Encoding^ temp = gcnew UTF8Encoding( true );
         while ( fs->Read( b, 0, b->Length ) > 0 )
         {
            Console::WriteLine( temp->GetString( b ) );
         }
      }
      finally
      {
         if ( fs )
            delete (IDisposable^)fs;
      }
   }
}
//This code produces output similar to the following; 
//results may vary based on the computer/file structure/etc.:
//
//This is to test the OpenWrite method.
//
//
//
//
//
//
//
//
//
//
//
//
using System;
using System.IO;
using System.Text;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\Temp\MyTest.txt";
        FileInfo fi = new FileInfo(path);

        // Open the stream for writing.
        using (FileStream fs = fi.OpenWrite()) 
        {
            Byte[] info = 
                new UTF8Encoding(true).GetBytes("This is to test the OpenWrite method.");

            // Add some information to the file.
            fs.Write(info, 0, info.Length);
        }

        // Open the stream and read it back.
        using (FileStream fs = fi.OpenRead()) 
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);

            while (fs.Read(b,0,b.Length) > 0) 
            {
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
}
//This code produces output similar to the following; 
//results may vary based on the computer/file structure/etc.:
//
//This is to test the OpenWrite method.
//
//
//
//
//
//
//
//
//
//
//
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\Temp\MyTest.txt"
        Dim fi As FileInfo = New FileInfo(path)
        Dim fs As FileStream

        ' Open the stream for writing.
        fs = fi.OpenWrite()
        Dim info As Byte() = _
            New UTF8Encoding(True).GetBytes("This is to test the OpenWrite method.")

        ' Add some information to the file.
        fs.Write(info, 0, info.Length)
        fs.Close()

        'Open the stream and read it back.
        fs = fi.OpenRead()
        Dim b(1023) As Byte
        Dim temp As UTF8Encoding = New UTF8Encoding(True)

        Do While fs.Read(b, 0, b.Length) > 0
            Console.WriteLine(temp.GetString(b))
        Loop
        fs.Close()
    End Sub
End Class
'This code produces output similar to the following; 
'results may vary based on the computer/file structure/etc.:
'
'This is to test the OpenWrite method.
'
'
'
'
'
'
'
'
'
'
'
'

注解

如果OpenWrite文件路径中已存在一个文件, 则该方法将打开该文件, 如果该文件不存在, 则创建一个新文件。The OpenWrite method opens a file if one already exists for the file path, or creates a new file if one does not exist. 对于现有文件, 它不会将新文本追加到现有文本。For an existing file, it does not append the new text to the existing text. 相反, 它会用新字符覆盖现有字符。Instead, it overwrites the existing characters with the new characters. 如果使用较短的字符串 (如 "第二次运行") 覆盖较长的字符串 (如 "这是 OpenWrite 方法的测试"), 则该文件将包含字符串的混合 ("OpenWrite 方法的第二个 runtest")。If you overwrite a longer string (such as "This is a test of the OpenWrite method") with a shorter string (like "Second run"), the file will contain a mix of the strings ("Second runtest of the OpenWrite method").

安全性

FileIOPermission
用于读取和写入文件。for reading and writing files. 关联的枚举Read:WriteAssociated enumerations: Read, Write

适用于