FileInfo.AppendText メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > FileInfo.AppendText メソッドの意味・解説 

FileInfo.AppendText メソッド

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

名前空間: 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

解説解説
使用例使用例

ファイルテキスト追加し、そのファイル読み取る例を次に示します

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 のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「FileInfo.AppendText メソッド」の関連用語

FileInfo.AppendText メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



FileInfo.AppendText メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS