VB.net : テキストファイルとキャラクタセット


  ダウンロードと概要




ブラウザでダウンロード

テキストファイルに日本語が記述されている場合、その日本語のキャラクタセットは様々で、
通常テキストエディタで使われている種類だけでも以下のようになります。

キャラクタセット

■ SHIFT_JIS
■ UTF-8( BOM あり )
■ UTF-8( BOM なし )
■ EUC-JP
■ UNICODE
■ JIS


VB.net の内部コードは UNICODE なので、それら変換してメモリに置かないと、正しく
日本語として読む事ができません。そして、その役割は、System.Text.Encoding クラス
が受け持つこととなります。その相互変換の基本コードを、Byte 配列を使う方法も含めて
列挙しています


実行結果の表示
  

vb.net>text
SHIFT_JIS から読んだものと、UTF-8 から読んだものは同じです
UTF8(BOM) から読んだものと、UTF8N から読んだものは同じです
UTF8N から読んだものと、EUC-JP から読んだものは同じです
EUC-JPから読んだものと、UNICODE から読んだものは同じです
StreamReader から読んだものと、FileStream から読んだものは同じです
1961
1869
-------------------------------------
************************************************************
*【実行方法】
************************************************************
__buildPath.txt に SDK用の正しいパスが必要ですが、だいたいに
おいて、Framework2.0 がインストールされておれば変更する必要
-------------------------------------
************************************************************
*【実行方法】
************************************************************
__buildPath.txt に SDK用の正しいパスが必要ですが、だいたいに
おいて、Framework2.0 がインストールされておれば変更する必要
-------------------------------------
************************************************************
*【実行方法】
************************************************************
__buildPath.txt に SDK用の正しいパスが必要ですが、だいたいに
おいて、Framework2.0 がインストールされておれば変更する必要
-------------------------------------
  

関連する記事

VB.net : String、Char()、Byte() の相互変換










  ソースコード




' ********************************************************
' ■ テキストファイルの処理
' ********************************************************
Imports System.IO
Imports System.Text

Module MyModule

' ********************************************************
' VB.net 内は Unicode ですから、それを中心に考えます
' 左側がファイルで、右側がメモリです
' ********************************************************
Sub Main()

	' ********************************************************
	' =============        =============
	' | SHIFT_JIS |   =>   |  UNICODE  |
	' =============        =============
	' ********************************************************
	Dim SJISFileIn As StreamReader = New StreamReader( "readme.txt", Encoding.GetEncoding(932) )
	' 全て読み込む
	Dim Text1 As String = SJISFileIn.ReadToEnd()
	' 閉じる
	SJISFileIn.Close()
	SJISFileIn.Dispose()


	' ********************************************************
	' =============        =============
	' | UTF8(BOM) |   <=   |  UNICODE  |
	' =============        =============
	' ********************************************************
	' BOM あり
	Dim UTF8BOM As UTF8Encoding = New UTF8Encoding(True)	' Encoding.UTF8 と同等
	' 第二引数 : False で上書き、True ならば追加
	Dim UTF8FileOut As StreamWriter = New StreamWriter( "readme_utf8.txt", False, UTF8BOM )
	UTF8FileOut.Write( Text1 )
	UTF8FileOut.Close()
	UTF8FileOut.Dispose()


	' ********************************************************
	' =============        =============
	' | UTF8(BOM) |   =>   |  UNICODE  |
	' =============        =============
	' ********************************************************
	Dim UTF8FileIn As StreamReader = New StreamReader( "readme_utf8.txt", UTF8BOM )
	' 全て読み込む
	Dim Text2 As String = UTF8FileIn.ReadToEnd()
	' 閉じる
	UTF8FileIn.Close()
	UTF8FileIn.Dispose()


	' ********************************************************
	' 内部表現の Unicode で 同じものとなります
	' ********************************************************
	if Text1 = Text2 then
		Console.WriteLine("SHIFT_JIS から読んだものと、UTF-8 から読んだものは同じです")
	end if


	' ********************************************************
	' UTF8N は 先頭に BOM がありません
	' UTF8Encoding のデフォルトの状態です
	' =============        =============
	' |   UTF8N   |   <=   |  UNICODE  |
	' =============        =============
	' ********************************************************
	' BOM なし
	Dim UTF8noBOM As UTF8Encoding = New UTF8Encoding()
	' 第二引数 : False で上書き、True ならば追加
	Dim UTF8NFileOut As StreamWriter = New StreamWriter( "readme_utf8n.txt", False, UTF8noBOM )
	UTF8NFileOut.Write( Text1 )
	UTF8NFileOut.Close()
	UTF8NFileOut.Dispose()


	' ********************************************************
	' =============        =============
	' |   UTF8N   |   =>   |  UNICODE  |
	' =============        =============
	' ********************************************************
	Dim UTF8NFileIn As StreamReader = New StreamReader( "readme_utf8n.txt", UTF8noBOM )
	' 全て読み込む
	Dim Text3 As String = UTF8NFileIn.ReadToEnd()
	' 閉じる
	UTF8NFileIn.Close()
	UTF8NFileIn.Dispose()

	' ********************************************************
	' 内部表現の Unicode で 同じものとなります
	' ********************************************************
	if Text2 = Text3 then
		Console.WriteLine("UTF8(BOM) から読んだものと、UTF8N から読んだものは同じです")
	end if


	' ********************************************************
	' =============        =============
	' |   EUC-JP  |   <=   |  UNICODE  |
	' =============        =============
	' ********************************************************
	Dim EUCJP As Encoding = Encoding.GetEncoding(51932)
	' 第二引数 : False で上書き、True ならば追加
	Dim EUCJPFileOut As StreamWriter = New StreamWriter( "readme_eucjp.txt", False, EUCJP )
	EUCJPFileOut.Write( Text3 )
	EUCJPFileOut.Close()
	EUCJPFileOut.Dispose()


	' ********************************************************
	' =============        =============
	' |  EUC-JP   |   =>   |  UNICODE  |
	' =============        =============
	' ********************************************************
	Dim EUCJPFileIn As StreamReader = New StreamReader( "readme_eucjp.txt", EUCJP )
	' 全て読み込む
	Dim Text4 As String = EUCJPFileIn.ReadToEnd()
	' 閉じる
	EUCJPFileIn.Close()
	EUCJPFileIn.Dispose()


	' ********************************************************
	' 内部表現の Unicode で 同じものとなります
	' ********************************************************
	if Text3 = Text4 then
		Console.WriteLine("UTF8N から読んだものと、EUC-JP から読んだものは同じです")
	end if


	' ********************************************************
	' =============        =============
	' |  UNICODE  |   <=   |  UNICODE  |
	' =============        =============
	' ********************************************************
	Dim UNICODE As Encoding = Encoding.Unicode 
	' 第二引数 : False で上書き、True ならば追加
	Dim UNICODEFileOut As StreamWriter = New StreamWriter( "readme_unicode.txt", False, UNICODE )
	UNICODEFileOut.Write( Text4 )
	UNICODEFileOut.Close()
	UNICODEFileOut.Dispose()


	' ********************************************************
	' =============        =============
	' |  UNICODE  |   =>   |  UNICODE  |
	' =============        =============
	' ********************************************************
	Dim UNICODEFileIn As StreamReader = New StreamReader( "readme_unicode.txt", UNICODE )
	' 全て読み込む
	Dim Text5 As String = UNICODEFileIn.ReadToEnd()
	' 閉じる
	UNICODEFileIn.Close()
	UNICODEFileIn.Dispose()


	' ********************************************************
	' 内部表現の Unicode で 同じものとなります
	' ********************************************************
	if Text4 = Text5 then
		Console.WriteLine("EUC-JPから読んだものと、UNICODE から読んだものは同じです")
	end if


	' ********************************************************
	' =============        =============
	' | SHIFT_JIS |   <=   |  UNICODE  |
	' =============        =============
	' ********************************************************
	Dim SHIFTJIS As Encoding = Encoding.GetEncoding(932)
	' 第二引数 : False で上書き、True ならば追加
	Dim SHIFTJISFileOut As StreamWriter = New StreamWriter( "readme_shiftjis.txt", False, SHIFTJIS )
	SHIFTJISFileOut.Write( Text5 )
	SHIFTJISFileOut.Close()
	SHIFTJISFileOut.Dispose()


	' ********************************************************
	' =============        =============        =============
	' | SHIFT_JIS |   =>   |   Byte()  |   =>   |  UNICODE  |
	' =============        =============        =============
	' ********************************************************
	Dim fs As FileStream = New FileStream( "readme_shiftjis.txt", FileMode.Open )
	Dim fb As Byte() = New Byte( fs.Length ){}
	fs.Read( fb, 0, fs.Length )
	' SHIFT_JIS で文字列化
	Dim Text6 As String = SHIFTJIS.GetString( fb, 0, fs.Length )
	fs.Close()
	fs.Dispose()

	' ********************************************************
	' 内部表現の Unicode で 同じものとなります
	' ********************************************************
	if Text5 = Text6 then
		Console.WriteLine("StreamReader から読んだものと、FileStream から読んだものは同じです")
	end if


	' ********************************************************
	' =============        =============        =============
	' |iso-2022-jp|   <=   |   Byte()  |   <=   |  UNICODE  |
	' =============        =============        =============
	' ********************************************************
	fb = Encoding.GetEncoding(50222).GetBytes(Text6)
	Dim fso As FileStream = New FileStream( "readme_jis.txt", FileMode.Create )
	fso.Write( fb, 0, fb.Length )
	fso.Close()
	fso.Dispose()


	' ********************************************************
	' 改行コードで分解する
	' 
	' 1) Windows では、CrLf なので、Cr を削除しておく
	' 2) Lf で分割する
	' ********************************************************
	Dim ByteCr As Byte() = New Byte() { 13 }
	Dim ByteLf As Byte() = New Byte() { 10 }

	' 文字列としての Cr
	Dim repl As String = Encoding.ASCII.GetString(ByteCr)

	' 置換前のテキストの量
	Console.WriteLine( Text5.Length )
	' 置換
	Text5 = Text5.Replace( repl,"" )
	' 置換後のテキストの量
	Console.WriteLine( Text5.Length )
	Console.WriteLine("-------------------------------------")

	' 分割用のセパレータを作成	
	Dim separator As Char() = Encoding.ASCII.GetChars(ByteLf)
	' 配列
	Dim lines As String()

	' Lf で分割して配列を作成
	lines = Text5.Split( separator )

	Dim cnt As Integer = 0

	' 一行づつ表示
	Dim line As String
	For Each line In lines
		cnt += 1
		if cnt > 5 then
			Exit For
		end if

		Console.WriteLine(line)
	Next
	Console.WriteLine("-------------------------------------")

	' ********************************************************
	' VB.net ではセパレータは文字列でも良い
	' ********************************************************
	Dim separator2 As String = Encoding.ASCII.GetString(ByteLf)
	' 文字列の Lf で分割して配列を作成
	lines = Text5.Split( separator2 )

	cnt = 0

	' 一行づつ表示
	For Each line In lines
		cnt += 1
		if cnt > 5 then
			Exit For
		end if

		Console.WriteLine(line)
	Next
	Console.WriteLine("-------------------------------------")


	' ********************************************************
	' VB.net 用の名前空間にある定数でも良い
	' ********************************************************
	lines = Text5.Split( Microsoft.VisualBasic.ControlChars.Lf )

	cnt = 0

	' 一行づつ表示
	For Each line In lines
		cnt += 1
		if cnt > 5 then
			Exit For
		end if

		Console.WriteLine(line)
	Next
	Console.WriteLine("-------------------------------------")



End Sub

End Module





yahoo  google  MSDN  MSDN(us)  WinFAQ  Win Howto  tohoho  ie_DHTML  vector  wdic  辞書  天気 


[dnettool]
claudebot
24/04/19 00:55:33
InfoBoard Version 1.00 : Language=Perl

1 BatchHelper COMprog CommonSpec Cprog CprogBase CprogSAMPLE CprogSTD CprogSTD2 CprogWinsock Cygwin GameScript HTML HTMLcss InstallShield InstallShieldFunc JScript JScriptSAMPLE Jsfuncs LLINK OldProg OracleGold OracleSilver PRO PRObrowser PROc PROconePOINT PROcontrol PROftpclient PROjscript PROmailer PROperl PROperlCHAT PROphp PROphpLesson PROphpLesson2 PROphpLesson3 PROphpfunction PROphpfunctionArray PROphpfunctionMisc PROphpfunctionString PROsql PROvb PROvbFunction PROvbString PROvbdbmtn PROvbonepoint PROwebapp PROwin1POINT PROwinSYSTEM PROwinYOROZU PROwindows ProjectBoard RealPHP ScriptAPP ScriptMaster VBRealtime Vsfuncs a1root access accreq adsi ajax amazon argus asp aspSample aspVarious aspdotnet aw2kinst cappvariety centura ckeyword classStyle cmaterial cmbin cmdbapp cmenum cmlang cmlistbox cmstd cmstdseed cmtxt cs daz3d db dbCommon dbaccess dnettool dos download flex2 flex3 flex4 framemtn framereq freeWorld freesoft gimp ginpro giodownload google hdml home hta htmlDom ie9svg install java javaSwing javascript jetsql jquery jsp jspTest jspVarious lightbox listasp listmsapi listmsie listmsiis listmsnt listmspatch listmsscript listmsvb listmsvc memo ms msde mysql netbeans oraPlsql oracle oracleWiper oraclehelper orafunc other panoramio pear perl personal pgdojo pgdojo_cal pgdojo_holiday pgdojo_idx pgdojo_ref pgdojo_req php phpVarious phpguide plsql postgres ps r205 realC realwebapp regex rgaki ruby rule sboard sc scprint scquest sdb sdbquest seesaa setup sh_Imagick sh_canvas sh_dotnet sh_google sh_tool sh_web shadowbox shgm shjquery shvbs shweb sjscript skadai skywalker smalltech sperl sqlq src systemdoc tcpip tegaki three toolbox twitter typeface usb useXML vb vbdb vbsfunc vbsguide vbsrc vpc wcsignup webanymind webappgen webclass webparts webtool webwsh win8 winofsql wmi work wp youtube