System.IO.StreamWriter.Write 方法 (Char[], Int32, Int32)

方法描述

将字符的子数组写入流。

语法定义(C# System.IO.StreamWriter.Write 方法 (Char[], Int32, Int32) 的用法)

public override void Write(
	char[] buffer,
	int index,
	int count
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
buffer System-Char[] 包含要写入的数据的字符数组。
index System-Int32 开始写入的 buffer 中的索引。
count System-Int32 将从 buffer 中读取的字符数。
返回值 void

提示和注释

此方法重写 TextWriter.Write。

从 index 的 buffer 开始处读取这些字符,并继续贯穿 index + (count - 1)。 所有字符被写入基础流,除非提前达到底层流的末尾。 如果 AutoFlush 为 true,将自动调用 Flush。

有关通用 I/O 任务的列表,请参见通用 I/O 任务。

System.IO.StreamWriter.Write 方法 (Char[], Int32, Int32)例子

此示例将 8 个字符从一个包含 13 个元素的数组写入文件(从数组的第三个元素开始)。

using System;
using System.IO;

public class SWBuff 
{
    public static void Main(String[] args)
    {
        FileStream sb = new FileStream("MyFile.txt", FileMode.OpenOrCreate);
        char[] b = {'a','b','c','d','e','f','g','h','i','j','k','l','m'};
        StreamWriter sw = new StreamWriter(sb);
        sw.Write(b, 3, 8);
        sw.Close();
    }
}

异常

异常 异常描述
ArgumentNullException buffer 为 null。
ArgumentException 缓冲区长度减去 index 小于 count。
ArgumentOutOfRangeException index 或 count 为负。
IOException 发生 I/O 错误。
ObjectDisposedException AutoFlush 为 true 或 StreamWriter 缓冲区已满,并且当前编写器已关闭。
NotSupportedException AutoFlush 为 true 或 StreamWriter 缓冲区已满,并且由于 StreamWriter 位于基础固定大小流的结尾,缓冲区的内容无法写入该流。

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。