C# の TextWriter

PHPz
リリース: 2024-09-03 15:22:45
オリジナル
187 人が閲覧しました

TextWriter は、ファイルにテキストを書き込むために使用されます。以下は C# の TextWriter に関する重要なポイントです。TextWriter は IO 名前空間の抽象クラスです。これは、一連の連続した文字をファイルに書き込むために使用されます。これは、StreamWriter と StringWriter の基本クラスで、それぞれストリームと文字列に文字を書き込むために使用されます。

デフォルトでは、スレッドセーフではありません。抽象クラスなのでオブジェクトを作成できません。 TextWriter を実装するクラスは、その有用なインスタンスを作成するために、最小限その Write(Char) メソッドを実装する必要があります。

説明付き構文

TextWriter text_writer = File.CreateText(file_path);
ログイン後にコピー
ログイン後にコピー

上記のステートメントは、指定された場所 (file_path) にファイルが存在しない場合に新しいファイルを作成します。次に、text_writer を使用して TextWriter クラスのメソッドを呼び出し、C# でファイルを簡単に操作できるようになります。

次のような using ステートメントを使用して TextWriter を作成できます。

using(TextWriter text_writer = File.CreateText(file_path))
{
//user code
}
ログイン後にコピー

TextWriter は、作業が完了して不要になったら、using ブロックで指定されたオブジェクトを解放するように .NET に指示するため、 using ステートメントを使用して TextWriter を使用することをお勧めします。

TextWriter は C# でどのように動作しますか?

TextWriter を使用するには、まず System.IO 名前空間をインポートする必要があります。 TextWriter は抽象クラスであるため、「new」キーワードを使用して TextWriter のインスタンスを直接作成することはできません。したがって、インスタンスを作成するには、次のような File クラスの CreateText() メソッドを使用します。

TextWriter text_writer = File.CreateText(file_path);
ログイン後にコピー
ログイン後にコピー
このメソッドは、書き込みのために開かれるファイルのパスを取得します。 UTF-8 エンコードされたテキストを書き込むためのファイルを作成または開きます。ファイルがすでに存在する場合、その内容は上書きされます。

これは、TextWriter の派生クラスである StreamWriter のオブジェクトを返すため、TextWriter クラスのインスタンスの作成に役立ちます。このインスタンスを利用して、TextWriter のメソッドを呼び出してテキストをファイルに書き込むことができます。

TextWriter は、抽象クラス MarshalByRefObject の派生クラスです。その継承階層は次のとおりです:

オブジェクト —–> MarshalByRefObject ——–>テキストライター

StreamWriter と同様に、TextWriter クラスから派生し、TextWriter のメンバーに実装を提供する他のクラスもあります。 TextWriter で使用できる派生クラスのリストを以下に示します。

  • IndentedTextWriter: タブ文字列を挿入し、現在のインデント レベルを追跡するために使用されます。
  • StreamWriter: 特定のエンコーディングでストリームに文字を書き込むために使用されます。
  • StringWriter: 文字列に情報を書き込むために使用されます。情報は基礎となる StringBuilder に保存されます。
  • HttpWriter: 組み込みの HttpResponse オブジェクトを通じてアクセスできる TextWriter クラスのオブジェクトを提供します。
  • HtmlTextWriter: マークアップ文字とテキストを ASP.NET サーバー コントロールの出力ストリームに書き込むために使用されます。
次に、TextWriter のいくつかの重要なメソッドについて説明します。

メソッド
Method Description
Close() It is used to close the current writer and it releases any system resources associated with that writer.
Dispose() It is used to release all the resources used by the TextWriter object.
Flush() It is used to clear all buffers for the current writer and causes any buffered data to be written to the underlying device.
Synchronized(TextWriter) It is used to create a thread-safe wrapper around the specified TextWriter.
Write(Char) It is used to write a character to the text stream.
Write(String) It is used to write the string to the text stream.
WriteAsync(Char) It is used to write the character to the text stream asynchronously.
WriteLine() It is used to write line terminator to the text stream.
WriteLineAsync(String) It is used to write the string to the text stream asynchronously followed by a line terminator.
説明 Close() 現在のライターを閉じるために使用され、そのライターに関連付けられているシステム リソースが解放されます。 破棄() TextWriter オブジェクトによって使用されるすべてのリソースを解放するために使用されます。 フラッシュ() 現在のライターのすべてのバッファをクリアするために使用され、バッファされたデータが基礎となるデバイスに書き込まれます。 同期済み(TextWriter) 指定された TextWriter の周囲にスレッドセーフなラッパーを作成するために使用されます。 書き込み(文字) テキスト ストリームに文字を書き込むために使用されます。 書き込み(文字列) テキスト ストリームに文字列を書き込むために使用されます。 WriteAsync(Char) テキスト ストリームに文字を非同期的に書き込むために使用されます。 WriteLine() テキスト ストリームに行末記号を書き込むために使用されます。 WriteLineAsync(String) これは、文字列をテキスト ストリームに非同期的に書き込むために使用され、その後に行終端文字が続きます。 テーブル>

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApp2
{
class Program
{
public static void Main()
{
string file = @"E:\Content\textWriter.txt";
// check if the file exists
try
{
if (File.Exists(file))
{
File.Delete(file);
}
// create the file
using (TextWriter writer = File.CreateText(file))
{
writer.WriteLine("TextWriter is an abstract class under " +
"System.IO namespace. It is used to write sequential " +
"series of characters into a file. It is the base class " +
"of StreamWriter and StringWriter which is used to " +
"write characters to streams and strings respectively. " +
"By default, it is not thread safe. " +
"As it is an abstract class, its object cannot be created. " +
"Any class implementing TextWriter must minimally implement " +
"its Write(Char) method to create its useful instance. ");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
ログイン後にコピー

Output:

C# の TextWriter

We can asynchronously write characters to stream by using WriteAsync(Char) method such as:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApp2
{
public class Program
{
public static void Main(string[] args)
{
WriteCharAsync();
}
public static async void WriteCharAsync()
{
string file = @"E:\Content\textWriterAsync.txt";
try
{
//check if file already exists
if (File.Exists(file))
{
File.Delete(file);
}
using (StreamWriter writer = File.CreateText(file))
{
await writer.WriteLineAsync("TextWriter is an abstract class under "+
"System.IO namespace. It is used to write sequential " +
"series of characters into a file. It is the base class " +
"of StreamWriter and StringWriter which is used to " +
"write characters to streams and strings respectively. " +
"By default, it is not thread safe. " +
"As it is an abstract class, its object cannot be created. " +
"Any class implementing TextWriter must minimally implement " +
"its Write(Char) method to create its useful instance. ");
await writer.WriteLineAsync("We are writing characters " +
"asynchronously.");
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
ログイン後にコピー

Output:

C# の TextWriter

Conclusion

TextWriter is used to write text or sequential series of characters to a file. A class derived from the TextWriter class needs to provide implementation to any of the members of the TextWriter. Write() methods of TextWriter with primitive data types as parameters write out values as strings.

以上がC# の TextWriterの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!