次の方法で共有


StreamWriter.Write メソッド (Char , Int32, Int32)

ストリームに文字配列の一部を書き込みます。

Overrides Overloads Public Sub Write( _
   ByVal buffer() As Char, _   ByVal index As Integer, _   ByVal count As Integer _)
[C#]
public override void Write(char[] buffer,intindex,intcount);
[C++]
public: void Write(__wchar_tbuffer __gc[],intindex,intcount);
[JScript]
public override function Write(
   buffer : Char[],index : int,count : int);

パラメータ

  • buffer
    書き込むデータを格納する文字配列。
  • index
    書き込みの開始位置を示す buffer 内のインデックス。
  • count
    buffer から読み込む文字数。

例外

例外の種類 条件
ArgumentNullException buffer が null 参照 (Visual Basic では Nothing) です。
ArgumentException バッファ長から index を差し引いた値が count より小さい値です。
ArgumentOutOfRangeException index または count が負の値です。
IOException I/O エラーが発生しました。
ObjectDisposedException AutoFlush が true または StreamWriter バッファがいっぱいで、現在のライタが閉じられています。
NotSupportedException AutoFlush が true または StreamWriter バッファがいっぱいで、 StreamWriter がストリームの末尾にあるため、基になる固定サイズのストリームにバッファの内容を書き込むことができません。

解説

このメソッドは、 TextWriter.Write をオーバーライドします。

buffer 内の index から始まる index + (count- 1) までの文字を読み取ります。早まって基になるストリームの末尾に到達しない限り、すべての文字が基になるストリームに書き込まれます。 AutoFlushtrue の場合は、 Flush が自動的に呼び出されます。

このメソッドの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。

実行するタスク 参考例があるトピック
テキスト ファイルを作成する。 ファイルへのテキストの書き込み
テキスト ファイルに書き込む。 ファイルへのテキストの書き込み
テキスト ファイルから読み取る。 ファイルからのテキストの読み取り
テキストをファイルに追加する。 ログ ファイルのオープンと追加

File.AppendText

FileInfo.AppendText

ファイルのサイズを取得する。 FileInfo.Length
ファイルの属性を取得する。 File.GetAttributes
ファイルの属性を設定する。 File.SetAttributes
ファイルが存在するかどうかを判別する。 File.Exists
バイナリ ファイルから読み取る。 新しく作成したデータ ファイルの読み取りと書き込み
バイナリ ファイルに書き込む。 新しく作成したデータ ファイルの読み取りと書き込み

使用例

[Visual Basic, C#, C++] この例では、13 要素から成る配列の 3 番目の要素から始まる 8 文字がファイルに書き込まれます。

 
Imports System
Imports System.IO

Public Class SWBuff

    Public Shared Sub Main()
        Dim sb As New FileStream("MyFile.txt", FileMode.OpenOrCreate)
        Dim b As Char() = {"a"c, "b"c, "c"c, "d"c, "e"c, "f"c, "g"c, _
           "h"c, "i"c, "j"c, "k"c, "l"c, "m"c}
        Dim sw As New StreamWriter(sb)
        sw.Write(b, 3, 8)
        sw.Close()
    End Sub
End Class

[C#] 
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();
    }
}

[C++] 

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

StreamWriter クラス | StreamWriter メンバ | System.IO 名前空間 | StreamWriter.Write オーバーロードの一覧 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み