FileInfo.AppendText 方法

创建一个 StreamWriter,它向 FileInfo 的此实例表示的文件追加文本。

**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Function AppendText As StreamWriter
用法
Dim instance As FileInfo
Dim returnValue As StreamWriter

returnValue = instance.AppendText
public StreamWriter AppendText ()
public:
StreamWriter^ AppendText ()
public StreamWriter AppendText ()
public function AppendText () : StreamWriter

返回值

新的 StreamWriter

备注

下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作...

请参见本主题中的示例...

创建文本文件。

如何:向文件写入文本

写入文本文件。

如何:向文件写入文本

读取文本文件。

如何:从文件读取文本

示例

下面的示例将文本追加到一个文件并从此文件中读取。

Imports System
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
using System;
using System.IO;

class Test 
{
    
    public static void Main() 
    {
        FileInfo fi = new FileInfo(@"c:\temp\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);
            }
        }
    }
}
using namespace System;
using namespace System::IO;

int main()
{
   FileInfo^ fi = gcnew FileInfo( "c:\\temp\\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;
   }
}
import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        FileInfo fi = new FileInfo("c:\\temp\\MyTest.txt");

        // This text is added only once to the file.
        if (!(fi.get_Exists())) {
            //Create a file to write to.
            StreamWriter sw = fi.CreateText();
            try {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }
            finally {
                sw.Dispose();
            }
        }

        // 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 {
            sw.Dispose();
        }

        //Open the file to read from.
        StreamReader sr = fi.OpenText();
        try {
            String s = "";
            while ((s = sr.ReadLine()) != null) {
                Console.WriteLine(s);
            }
        }
        finally {
            sr.Dispose();
        }
    } //main
} //Test

下面的示例说明如何将文本追加到文件末尾并在控制台显示追加操作的结果。第一次调用此例程时,如果文件不存在则创建该文件。然后,将指定的文本追加到该文件。

Imports System
Imports System.IO

Public Class AppendTextTest
    Public Shared Sub Main()
        Dim fi As New FileInfo("temp.txt")
        Dim sw As StreamWriter = fi.AppendText()
        ' Create a reference to a file, which might or might not exist.
        ' If it does not exist, it is not yet created.
        ' Create a writer, ready to add entries to the file.
        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 'Main
End Class 'AppendTextTest
using System;
using System.IO;

public class AppendTextTest 
{
    public static void Main() 
    {
        // Create a reference to a file, which might or might not exist.
        // If it does not exist, it is not yet created.
        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() );
    }
}
using namespace System;
using namespace System::IO;
int main()
{
   
   // Create a reference to a file, which might or might not exist.
   // If it does not exist, it is not yet created.
   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() );
}
import System;
import System.IO;

public class AppendTextTest {
    public static function Main() : void {

        // Create a reference to a file, which might or might not exist.
        // If it does not exist, it is not yet created.
        var fi : FileInfo = new FileInfo("temp.txt");

        // Create a writer, ready to add entries to the file.
        var sw : StreamWriter = 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.
        var sr : StreamReader = new StreamReader( fi.OpenRead() );

        while (sr.Peek() != -1)
            Console.WriteLine( sr.ReadLine() );
    }
}
AppendTextTest.Main();

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

FileInfo 类
FileInfo 成员
System.IO 命名空间
StreamWriter

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本
基本的文件 I/O
如何:对新建的数据文件进行读取和写入