FileInfo.AppendText 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
StreamWriter의 이 인스턴스가 나타내는 파일에 텍스트를 추가하는 FileInfo를 만듭니다.
public:
System::IO::StreamWriter ^ AppendText();
public System.IO.StreamWriter AppendText ();
member this.AppendText : unit -> System.IO.StreamWriter
Public Function AppendText () As StreamWriter
반환
새 StreamWriter
입니다.
예제
다음 예제에서는 파일에 텍스트를 추가하고 파일에서 읽습니다.
using namespace System;
using namespace System::IO;
int main()
{
FileInfo^ fi = gcnew FileInfo( "c:\\MyTest.txt" );
// This text is added only once to the file.
if ( !fi->Exists )
{
//Create a file to write to.
StreamWriter^ sw = fi->CreateText();
try
{
sw->WriteLine( "Hello" );
sw->WriteLine( "And" );
sw->WriteLine( "Welcome" );
}
finally
{
if ( sw )
delete (IDisposable^)sw;
}
}
// This text will always be added, making the file longer over time
// if it is not deleted.
StreamWriter^ sw = fi->AppendText();
try
{
sw->WriteLine( "This" );
sw->WriteLine( "is Extra" );
sw->WriteLine( "Text" );
}
finally
{
if ( sw )
delete (IDisposable^)sw;
}
//Open the file to read from.
StreamReader^ sr = fi->OpenText();
try
{
String^ s = "";
while ( s = sr->ReadLine() )
{
Console::WriteLine( s );
}
}
finally
{
if ( sr )
delete (IDisposable^)sr;
}
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//Hello
//And
//Welcome
//This
//is Extra
//Text
//
//When you run this application a second time, you will see the following output:
//
//Hello
//And
//Welcome
//This
//is Extra
//Text
//This
//is Extra
//Text
using System;
using System.IO;
class Test
{
public static void Main()
{
FileInfo fi = new FileInfo(@"c:\MyTest.txt");
// This text is added only once to the file.
if (!fi.Exists)
{
//Create a file to write to.
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
// This text will always be added, making the file longer over time
// if it is not deleted.
using (StreamWriter sw = fi.AppendText())
{
sw.WriteLine("This");
sw.WriteLine("is Extra");
sw.WriteLine("Text");
}
//Open the file to read from.
using (StreamReader sr = fi.OpenText())
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//Hello
//And
//Welcome
//This
//is Extra
//Text
//When you run this application a second time, you will see the following output:
//
//Hello
//And
//Welcome
//This
//is Extra
//Text
//This
//is Extra
//Text
Imports System.IO
Public Class Test
Public Shared Sub Main()
Dim fi As FileInfo = New FileInfo("c:\temp\MyTest.txt")
Dim sw As StreamWriter
' This text is added only once to the file.
If fi.Exists = False Then
'Create a file to write to.
sw = fi.CreateText()
sw.WriteLine("Hello")
sw.WriteLine("And")
sw.WriteLine("Welcome")
sw.Flush()
sw.Close()
End If
' This text will always be added, making the file longer over time
' if it is not deleted.
sw = fi.AppendText()
sw.WriteLine("This")
sw.WriteLine("is Extra")
sw.WriteLine("Text")
sw.Flush()
sw.Close()
'Open the file to read from.
Dim sr As StreamReader = fi.OpenText()
Dim s As String
Do While sr.Peek() >= 0
s = sr.ReadLine()
Console.WriteLine(s)
Loop
sr.Close()
End Sub
End Class
'This code produces output similar to the following;
'results may vary based on the computer/file structure/etc.:
'
'Hello
'And
'Welcome
'This
'is Extra
'Text
'When you run this application a second time, you will see the following output:
'
'Hello
'And
'Welcome
'This
'is Extra
'Text
'This
'is Extra
'Text
다음 예제에서는 파일의 끝에 텍스트를 추가하고 콘솔에 추가 작업의 결과를 표시하는 방법을 보여 줍니다. 이 루틴이 처음 호출되면 파일이 없으면 만들어집니다. 그런 다음 지정된 텍스트가 파일에 추가됩니다.
using namespace System;
using namespace System::IO;
int main()
{
FileInfo^ fi = gcnew FileInfo( "temp.txt" );
// Create a writer, ready to add entries to the file.
StreamWriter^ sw = fi->AppendText();
sw->WriteLine( "Add as many lines as you like..." );
sw->WriteLine( "Add another line to the output..." );
sw->Flush();
sw->Close();
// Get the information out of the file and display it.
// Remember that the file might have other lines if it already existed.
StreamReader^ sr = gcnew StreamReader( fi->OpenRead() );
while ( sr->Peek() != -1 )
Console::WriteLine( sr->ReadLine() );
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//Add as many lines as you like...
//Add another line to the output...
}
using System;
using System.IO;
public class AppendTextTest
{
public static void Main()
{
FileInfo fi = new FileInfo("temp.txt");
// Create a writer, ready to add entries to the file.
StreamWriter sw = fi.AppendText();
sw.WriteLine("Add as many lines as you like...");
sw.WriteLine("Add another line to the output...");
sw.Flush();
sw.Close();
// Get the information out of the file and display it.
// Remember that the file might have other lines if it already existed.
StreamReader sr = new StreamReader(fi.OpenRead());
while (sr.Peek() != -1)
Console.WriteLine( sr.ReadLine() );
}
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//Add as many lines as you like...
//Add another line to the output...
Imports System.IO
Public Class AppendTextTest
Public Shared Sub Main()
Dim fi As New FileInfo("temp.txt")
Dim sw As StreamWriter = fi.AppendText()
sw.WriteLine("Add as many lines as you like...")
sw.WriteLine("Add another line to the output...")
sw.Flush()
sw.Close()
Dim sr As New StreamReader(fi.OpenRead())
' Get the information out of the file and display it.
' Remember that the file might have other lines if it already existed.
While sr.Peek() <> -1
Console.WriteLine(sr.ReadLine())
End While
End Sub
End Class
'This code produces output similar to the following;
'results may vary based on the computer/file structure/etc.:
'Add as many lines as you like...
'Add another line to the output...
적용 대상
추가 정보
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET