FileInfo.OpenWrite 메서드

정의

쓰기 전용 FileStream을 만듭니다.

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

반환

새 파일 또는 기존 파일의 공유되지 않는 쓰기 전용 FileStream 개체입니다.

예외

FileInfo 개체의 인스턴스를 만들 때 지정된 경로가 읽기 전용이거나 디렉터리입니다.

FileInfo 개체의 인스턴스를 만들 때 지정된 경로가 유효하지 않습니다(예: 매핑되지 않은 드라이브에 있음).

예제

다음 예제에서는 쓰기용 파일을 연 다음 파일에서 읽습니다.

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 파일 경로에 대한 파일이 이미 있는 경우 파일을 열거나 파일이 없는 경우 새 파일을 만듭니다. 기존 파일의 경우 기존 텍스트에 새 텍스트를 추가하지 않습니다. 대신 기존 문자를 새 문자로 덮어씁니다. 더 짧은 문자열(예: "두 번째 실행")을 사용하여 더 긴 문자열(예: "OpenWrite 메서드의 테스트")을 덮어쓰는 경우 파일에는 문자열("OpenWrite 메서드의 두 번째 runtest")이 혼합되어 포함됩니다.

적용 대상