SHIFT_JIS で入力して UTF8N で出力する :【VB.NET】テキストファイルの入出力

見どころは、タイトルで示した(UTF8N で出力という)内容です。

他はどこにでもあるサンプルです

クラスの使い方としては、StreamReader( StreamWriter ) + ファイルのパス
で、ファイルの OPEN をしていると考えるのがすっきりします。

ReadLine や WriteLine も使用できますし、Close メソッドで閉じた後、
Dispose で使用したリソースの後処理を行います。

当然、書き込み側では Flush というメソッドが存在し、即時書き込み
が必要な場合に使用します。
Imports System.IO
Imports System.Text

Module MyModule

' ********************************************************
' * 実行
' ********************************************************
Sub Main()

	Dim argv As String()

	' コマンドラインの配列( argv( 0 ) は、入力されたこのプログラム )
	argv = System.Environment.GetCommandLineArgs()
	Console.WriteLine( argv( 0 ) )

	if argv.Length <= 1 then
		Console.WriteLine("ファイルを指定して下さい")
		Return
	end if

	' ******************************************************
	' SJIS => UTF8N、UTF8 変換
	' ******************************************************
	Try
		' SHIFT_JIS 
		Dim ReadFile As StreamReader = _
			New StreamReader( argv(1), Encoding.GetEncoding(932) )

		' UTF8Encoding のコンストラクタはデフォルトでは BOM 無し
		Dim utf8NoBOM As New UTF8Encoding()
		Dim utf8BOM As New UTF8Encoding(True)	' Encoding.UTF8 と同等

		' BOM 無し で書き込む Writer を作成
		' 第二引数 : False で上書き、True ならば追加
		Dim WriteFile_utf8n As StreamWriter = _
			New StreamWriter( argv(1)+".utf8n.txt", False, utf8NoBOM )

		' BOM あり で書き込む Writer を作成
		Dim WriteFile_utf8 As StreamWriter = _
			New StreamWriter( argv(1)+".utf8.txt", False, utf8BOM )


		' SHIFT_JIS で読み込み
		Dim LineText As String = ReadFile.ReadLine()
		Do While Not LineText is Nothing

			' UTF8N で書き込み
			WriteFile_utf8n.WriteLine( LineText )
			' UTF8 で書き込み
			WriteFile_utf8.WriteLine( LineText )

			' 次行読み込み
			LineText = ReadFile.ReadLine()

		Loop

		' 終了処理
		WriteFile_utf8n.Close()
		WriteFile_utf8.Close()
		ReadFile.Close()

		ReadFile.Dispose()
		WriteFile_utf8n.Dispose()
		WriteFile_utf8.Dispose()

	' e が必要無ければ e As Exception を書かなくて良い
	Catch ' e As Exception
		' Console.WriteLine( e.Message )
	End Try


	' ******************************************************
	' SJIS => EUC-JP、UNICODE 一括読み書き
	' ******************************************************
	Try
		' SHIFT_JIS 
		Dim ReadFile As StreamReader = _
			New StreamReader( argv(1), Encoding.GetEncoding(932) )

		' EUC-JP 用
		Dim WriteFile_ujis As StreamWriter = New StreamWriter( _
			argv(1)+".ujis.txt", _
			False, Encoding.GetEncoding(51932) _
		)

		' UNICODE 用
		Dim WriteFile_utf16 As StreamWriter = New StreamWriter( _
			argv(1)+".unicode.txt", _
			False, Encoding.GetEncoding(1200) _
		)

		' 一括読み書き
		Dim Text As String = ReadFile.ReadToEnd()
		WriteFile_ujis.Write( Text )
		WriteFile_utf16.Write( Text )

		' 終了処理
		WriteFile_ujis.Close()
		WriteFile_utf16.Close()
		ReadFile.Close()

		ReadFile.Dispose()
		WriteFile_ujis.Dispose()
		WriteFile_utf16.Dispose()

	' e が必要無ければ e As Exception を書かなくて良い
	Catch ' e As Exception
		' Console.WriteLine( e.Message )
	End Try

End Sub

End Module


XML のテキスト保存 ( UTF8N )
方法としては前述と同じで、UTF8N 用の Encoding を作成して、
StreamWriter を、XmlDocument.Save に渡して、UTF8N で保存します
Imports System.IO
Imports System.Text

Module MyModule

' ********************************************************
' * 実行
' ********************************************************
Sub Main()

	Dim Doc As New Xml.XmlDocument()

	Dim strData As String = _
		"<?xml version=""1.0"" encoding=""UTF-8""?>" + _
		"<data>" + _
		"<row>" + _
		"<col type=""氏名"">山田 太郎</col>" + _
		"<col type=""フリガナ"">ヤマダ タロウ</col>" + _
		"<col type=""NO"">1234567890</col>" + _
		"</row>" + _
		"</data>"

	Doc.LoadXml(strData)

	' BOM あり
	Doc.Save( "mydata_bom.xml" )

	' BOM 無し
	Dim utf8NoBOM As New UTF8Encoding()
	Dim xmlWriter As StreamWriter = New StreamWriter( "mydata.xml", False, utf8NoBOM )
	Doc.Save( xmlWriter )
	xmlWriter.Close()
	xmlWriter.Dispose()

End Sub

End Module