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

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

FileInfo.CreateText メソッド

新しテキスト ファイル書き込みを行う StreamWriter を作成します

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function CreateText As
 StreamWriter
Dim instance As FileInfo
Dim returnValue As StreamWriter

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

戻り値
新しStreamWriter

例外例外
例外種類条件

UnauthorizedAccessException

指定したファイル名ディレクトリです。

IOException

ディスク読み取り専用です。

SecurityException

呼び出し元に必要なアクセス許可がありません。

解説解説
使用例使用例

CreateText メソッドの例を次に示します

Imports System
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)

        If fi.Exists = False Then
            'Create a file to write to.
            Dim sw As StreamWriter = fi.CreateText()
            sw.WriteLine("Hello")
            sw.WriteLine("And")
            sw.WriteLine("Welcome")
            sw.Flush()
            sw.Close()
        End If

        'Open the file to read from.
        Dim sr As StreamReader = fi.OpenText()

        Do While sr.Peek() >= 0
            Console.WriteLine(sr.ReadLine())
        Loop
        sr.Close()
    End Sub
End Class
using System;
using System.IO;

class Test 
{

    public static void Main()
 
    {
        string path = @"c:\temp\MyTest.txt";
        FileInfo fi = new FileInfo(path);

        if (!fi.Exists) 
        {
            //Create a file to write to.
            using (StreamWriter sw = fi.CreateText()) 
            {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }
        }

        //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()
{
   String^ path = "c:\\temp\\MyTest.txt";
   FileInfo^ fi = gcnew FileInfo( path );
   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;
      }
   }

   //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)
    {
        String path = "c:\\temp\\MyTest.txt";
        FileInfo fi = new FileInfo(path);

        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();
            }
        }

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



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

辞書ショートカット

すべての辞書の索引

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

   

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



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

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

©2024 GRAS Group, Inc.RSS