以下のように指定した文字列を使用して条件コンパイルを行います vbc.exe vbc_textfile.vb /define:SJIS_BUILD 主なエンコード として以下の5つのエンコードを使用します
1) SHIFT_JIS 2) EUC-JP 3) UNICODE 4) UTF-8N 5) UTF-8 ( BOM付き )( パッケージ内に簡易ダンプを同梱してますので、インストールして BOM を確認できます ) 比較的小さなファイルの場合は、ReadFile.ReadToEnd() で一度に全てメモリに 取得するのが簡単ですが、大きなファイルは行単位で処理するのが通常です。
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
' 入力ファイルのパス ( "入力ファイルのパス" と指定する )
Dim arguments As String() = Environment.GetCommandLineArgs()
' 引数は一つのみ許可
if arguments.Length <> 2 then
Console.WriteLine("引数を指定して下さい")
Return
end if
' 引数から取得
Dim BaseFilePath As String = arguments(1)
' **************************************************
' 主なエンコード
' **************************************************
' SHIFT_JIS
Dim SJIS_Enc As Encoding = Encoding.GetEncoding(932)
' EUC-JP
Dim UJIS_Enc As Encoding = Encoding.GetEncoding(51932)
' UNICODE 用
Dim UNI_Enc As Encoding = Encoding.GetEncoding(1200)
' UTF-8N
Dim UTF8N_Enc As New UTF8Encoding()
' UTF-8
Dim UTF8_Enc As New UTF8Encoding(True)
' **************************************************
' SHIFT_JISで読み、SHIFT_JIS で出力する
' **************************************************
' 読み込み用
Dim ReadFile As StreamReader = New StreamReader( BaseFilePath, SJIS_Enc )
' 書き込み用( 第二引数が False なので、上書き )
#If SJIS_BUILD Then
Dim WriteFile As StreamWriter = New StreamWriter( BaseFilePath + ".txt", False, SJIS_Enc )
#End If
#If UJIS_BUILD Then
Dim WriteFile As StreamWriter = New StreamWriter( BaseFilePath + ".txt", False, UJIS_Enc )
#End If
#If UNI_BUILD Then
Dim WriteFile As StreamWriter = New StreamWriter( BaseFilePath + ".txt", False, UNI_Enc )
#End If
#If UTF8N_BUILD Then
Dim WriteFile As StreamWriter = New StreamWriter( BaseFilePath + ".txt", False, UTF8N_Enc )
#End If
#If UTF8_BUILD Then
Dim WriteFile As StreamWriter = New StreamWriter( BaseFilePath + ".txt", False, UTF8_Enc )
#End If
' SHIFT_JIS で読み込み
Dim LineText As String = Nothing
' Peek() は、StreamReader オブジェクトの現在位置は変わりません
' それ以上読み取り可能な文字がない場合、戻り値は -1 です。
' 以下は、MSDN ライブラリでのサンプルコードと同じものです
Do While ReadFile.Peek() >= 0
LineText = ReadFile.ReadLine()
WriteFile.WriteLine( LineText )
Loop
' **************************************************
' 終了処理
' **************************************************
WriteFile.Close()
WriteFile.Dispose()
ReadFile.Close()
ReadFile.Dispose()
Console.WriteLine("処理が終了しました")
' 一時停止
Console.Write("Enterキーを押して下さい : ")
Console.ReadLine()
End Sub
End Module
タグ:VB.NET
|
|
【VB.NET : ベーシックの最新記事】
- VB.net : 「ファイルを開くダイアログ」のパラメータを単純なテキストファイルより取得する
- VB.net での DateDiff メソッドの使用方法
- VB.net : 整数 : カンマ編集/前ゼロ/前スペース/16進数文字列変換
- テキストファイルとキャラクタセットの処理
- VB.net : String、Char()、Byte() の相互変換
- VB.net : 文字単位の ASCII と Unicode
- コマンドプロンプト : 入力したキーストロークを表示しない readkey.exe
- VB.net/C# : 連続する文字の作成と良く使う文字列フォーマット
- VB.net : 各種ディレクトリパスの取得 : テンポラリ/システム/特殊ディレクトリの列挙
- VB.net : アプリケーションを開始した実行可能ファイルのパス
- VB.net : アプリケーションディレクトリ
- VB.NET : バッチファイル用ファイルの参照ダイアログ
- コマンドプロンプト : クリップボードにあるファイル情報を標準出力へ
- VB.NET(コマンドプロンプト) : 指定した文字列だけ色を変えて表示する
- VB.NET : SHA256







