方法 : ファイルにテキストを書き込む

テキスト ファイルにテキストを書き込む方法を次の例に示します。 "*.txt" 検索パターンを使用してユーザーの [マイ ドキュメント] フォルダーからすべてのテキスト ファイルを読み取り、それらを大きなテキスト ファイルに書き込みます。

メモメモ

Visual Basic のユーザーは、ファイル I/O 用の Microsoft.VisualBasic.FileIO.FileSystem クラスに用意されているメソッドとプロパティを使用することもできます。

使用例

Imports System
Imports System.IO
Imports System.Text
Imports System.Collections.Generic

Class Program

    Public Shared Sub Main(ByVal args As String())

        Dim mydocpath As String = _
        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        Dim sb As New StringBuilder()

        For Each txtName As String _
            In Directory.EnumerateFiles(mydocpath, "*.txt")
            Using sr As New StreamReader(txtName)
                sb.AppendLine(txtName.ToString())
                sb.AppendLine("= = = = = =")
                sb.Append(sr.ReadToEnd())
                sb.AppendLine()
                sb.AppendLine()

            End Using
        Next

        Using outfile As New StreamWriter(mydocpath & "\AllTxtFiles.txt")
            outfile.Write(sb.ToString())
        End Using
    End Sub
End Class
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;

class Program
{

    static void Main(string[] args)
    {

        string mydocpath = 
            Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        StringBuilder sb = new StringBuilder();

        foreach (string txtName in Directory.EnumerateFiles(mydocpath,"*.txt"))
        {
            using (StreamReader sr = new StreamReader(txtName))
            {
                sb.AppendLine(txtName.ToString());
                sb.AppendLine("= = = = = =");
                sb.Append(sr.ReadToEnd());
                sb.AppendLine();
                sb.AppendLine();
            }

        }

        using (StreamWriter outfile = 
            new StreamWriter(mydocpath + @"\AllTxtFiles.txt"))
        {
            outfile.Write(sb.ToString());
        }
    }
}
using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Collections::Generic;

ref class Program
{
public:
    static void Main()
    {
        String^ mydocpath =
            Environment::GetFolderPath(Environment::SpecialFolder::MyDocuments);
        StringBuilder^ sb = gcnew StringBuilder();

        for each (String^ txtName in Directory::EnumerateFiles(mydocpath, "*.txt"))
        {
            StreamReader^ sr = gcnew StreamReader(txtName);
            sb->AppendLine(txtName->ToString());
            sb->AppendLine("= = = = = =");
            sb->Append(sr->ReadToEnd());
            sb->AppendLine();
            sb->AppendLine();
            sr->Close();
        }

        StreamWriter^ outfile = gcnew StreamWriter(mydocpath + "\\AllTxtFiles.txt");
        outfile->Write(sb->ToString());
        outfile->Close();
    }
};

int main()
{
    Program::Main();
}

参照

処理手順

方法 : ディレクトリ一覧を作成する

方法 : 新しく作成されたデータ ファイルに対して読み書きする

方法 : ログ ファイルを開いて情報を追加する

方法 : ファイルからテキストを読み取る

方法 : 文字列から文字を読み取る

方法 : 文字列に文字を書き込む

参照

StreamWriter

File.CreateText

概念

基本のファイル I/O