FileInfo.AppendText メソッド

FileInfo のインスタンスが表すファイルの末尾にテキストを追加する StreamWriter を作成します。

Public Function AppendText() As StreamWriter
[C#]
public StreamWriter AppendText();
[C++]
public: StreamWriter* AppendText();
[JScript]
public function AppendText() : StreamWriter;

戻り値

新しい StreamWriter

解説

このメソッドの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する 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

[C#] 
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);
            }
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    FileInfo* fi = new FileInfo(S"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(S"Hello");
            sw->WriteLine(S"And");
            sw->WriteLine(S"Welcome");
        } __finally {
            if (sw) __try_cast<IDisposable*>(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(S"This");
        sw->WriteLine(S"is Extra");
        sw->WriteLine(S"Text");

    } __finally {
        if (sw) __try_cast<IDisposable*>(sw)->Dispose();
    }    

    //Open the file to read from.
    StreamReader* sr = fi->OpenText();
    try {
        String* s = S"";
        while (s = sr->ReadLine()) {
            Console::WriteLine(s);
        }
    } __finally {
        if (sr) __try_cast<IDisposable*>(sr)->Dispose();
    }
}

ファイルの末尾にテキストを追加し、追加操作の結果をコンソールに表示する例を次に示します。このルーチンを始めて呼び出したときにファイルが存在しなければ、ファイルが作成されます。その後、指定したテキストがファイルに追加されます。

 
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

[C#] 
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() );
    }
}

[C++] 
#using <mscorlib.dll>

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 = new FileInfo(S"temp.txt");
    // Create a writer, ready to add entries to the file.
    StreamWriter* sw = fi->AppendText();
    sw->WriteLine(S"Add as many lines as you like...");
    sw->WriteLine(S"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() );
}

[JScript] 
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();

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

.NET Framework セキュリティ:

参照

FileInfo クラス | FileInfo メンバ | System.IO 名前空間 | StreamWriter | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み | 基本のファイル I/O | 新しく作成したデータ ファイルの読み取りと書き込み