TextWriter 类
表示可以编写一个有序字符系列的编写器。该类为抽象类。
**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public MustInherit Class TextWriter
Inherits MarshalByRefObject
Implements IDisposable
用法
Dim instance As TextWriter
[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class TextWriter : MarshalByRefObject, IDisposable
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class TextWriter abstract : public MarshalByRefObject, IDisposable
/** @attribute SerializableAttribute() */
/** @attribute ComVisibleAttribute(true) */
public abstract class TextWriter extends MarshalByRefObject implements IDisposable
SerializableAttribute
ComVisibleAttribute(true)
public abstract class TextWriter extends MarshalByRefObject implements IDisposable
备注
TextWriter 是 StreamWriter 和 StringWriter 的抽象基类,它们将字符分别写入流和字符串。创建一个 TextWriter 实例,将对象写入字符串,或将字符串写入文件,或序列化 XML。也可使用 TextWriter 的实例将文本写入自定义后备存储区(所使用的 API 与用于字符串或流的 API 相同),或者增加对文本格式化的支持。
TextWriter 的所有使用基元数据类型作为参数的 Write 方法都将值作为字符串写出。
默认情况下,TextWriter 不是线程安全的。有关线程安全包装的信息,请参见 TextWriter.Synchronized。
为了生成有用的 TextWriter 实例,派生类必须至少实现 Write 方法。
下表列出了其他典型或相关的 I/O 任务的示例。
若要执行此操作... |
请参见本主题中的示例... |
---|---|
创建文本文件。 |
|
写入文本文件。 |
|
读取文本文件。 |
|
向文件中追加文本。 |
|
获取文件大小。 |
|
获取文件属性。 |
|
设置文件属性。 |
|
确定文件是否存在。 |
|
读取二进制文件。 |
|
写入二进制文件。 |
示例
下面的代码示例阐释了 TextReader 和 TextWriter 两种类型的多态行为。由于 aStringWriter 和 aStreamWriter 都属于 TextWriter 类型,因此 WriteVowel 方法会同时用这两个对象调用,并且会执行与每个特定类型关联的 Write 方法。同样,ReadText 方法会同时用 aStringReader 和 aStreamReader 调用,并且会执行适当的 ReadToEnd 方法。请注意,对于 aStringWriter 和 aStringReader,备份是字符串;而对于 aStreamWriter 和 aStreamReader,备份是文件。
Imports System
Imports System.IO
Public Class TextRW
Shared Sub Main()
Dim aStringWriter, aStreamWriter As TextWriter
aStringWriter = New StringWriter()
aStreamWriter = New StreamWriter("InvalidPathChars.txt")
WriteText(aStringWriter)
WriteText(aStreamWriter)
aStreamWriter.Close()
Dim aStringReader, aStreamReader As TextReader
aStringReader = New StringReader(aStringWriter.ToString())
aStreamReader = New StreamReader("InvalidPathChars.txt")
ReadText(aStringReader)
ReadText(aStreamReader)
aStreamReader.Close()
End Sub
Shared Sub WriteText(aTextWriter As TextWriter)
aTextWriter.Write("Invalid file path characters are: ")
aTextWriter.Write(Path.InvalidPathChars)
aTextWriter.Write("."C)
End Sub
Shared Sub ReadText(aTextReader As TextReader)
Console.WriteLine("From {0} - {1}", _
aTextReader.GetType().Name, aTextReader.ReadToEnd())
End Sub
End Class
using System;
using System.IO;
class TextRW
{
static void Main()
{
TextWriter stringWriter = new StringWriter();
using(TextWriter streamWriter =
new StreamWriter("InvalidPathChars.txt"))
{
WriteText(stringWriter);
WriteText(streamWriter);
}
TextReader stringReader =
new StringReader(stringWriter.ToString());
using(TextReader streamReader =
new StreamReader("InvalidPathChars.txt"))
{
ReadText(stringReader);
ReadText(streamReader);
}
}
static void WriteText(TextWriter textWriter)
{
textWriter.Write("Invalid file path characters are: ");
textWriter.Write(Path.InvalidPathChars);
textWriter.Write('.');
}
static void ReadText(TextReader textReader)
{
Console.WriteLine("From {0} - {1}",
textReader.GetType().Name, textReader.ReadToEnd());
}
}
using namespace System;
using namespace System::IO;
void WriteText( TextWriter^ textWriter )
{
textWriter->Write( "Invalid file path characters are: " );
textWriter->Write( Path::InvalidPathChars );
textWriter->Write( Char::Parse( "." ) );
}
void ReadText( TextReader^ textReader )
{
Console::WriteLine( "From {0} - {1}", textReader->GetType()->Name, textReader->ReadToEnd() );
}
int main()
{
TextWriter^ stringWriter = gcnew StringWriter;
TextWriter^ streamWriter = gcnew StreamWriter( "InvalidPathChars.txt" );
WriteText( stringWriter );
WriteText( streamWriter );
streamWriter->Close();
TextReader^ stringReader = gcnew StringReader( stringWriter->ToString() );
TextReader^ streamReader = gcnew StreamReader( "InvalidPathChars.txt" );
ReadText( stringReader );
ReadText( streamReader );
streamReader->Close();
}
import System.*;
import System.IO.*;
class TextRW
{
public static void main(String[] args)
{
TextWriter stringWriter = new StringWriter();
TextWriter streamWriter = new StreamWriter("InvalidPathChars.txt");
try {
WriteText(stringWriter);
WriteText(streamWriter);
}
finally {
streamWriter.Dispose();
}
TextReader stringReader = new StringReader(stringWriter.ToString());
TextReader streamReader = new StreamReader("InvalidPathChars.txt");
try {
ReadText(stringReader);
ReadText(streamReader);
}
finally {
streamReader.Dispose();
}
} //main
static void WriteText(TextWriter textWriter)
{
textWriter.Write("Invalid file path characters are: ");
textWriter.Write(Path.InvalidPathChars);
textWriter.Write('.');
} //WriteText
static void ReadText(TextReader textReader)
{
Console.WriteLine("From {0} - {1}",
textReader.GetType().get_Name(),textReader.ReadToEnd());
} //ReadText
} //TextRW
继承层次结构
System.Object
System.MarshalByRefObject
System.IO.TextWriter
System.CodeDom.Compiler.IndentedTextWriter
System.IO.StreamWriter
System.IO.StringWriter
System.Web.HttpWriter
System.Web.UI.HtmlTextWriter
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
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