' ********************************************************
' ■ テキストファイルの処理
' ********************************************************
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