Shift-JISとUTF-8でテキストファイルを読み書きする関数とサブルーチンです.
目次
- Shift-JISで読み込む
- UTF-8で読み込む
- Shift-JISで書き出す
- UTF-8で書き出す
- 変換
Shift-JISで読み込む
Private Function ReadShiftJisFile(ByVal filePath As String)
tmp = "" Open filePath For Input As #1 Do Until EOF(1) Line Input #1, buf tmp = tmp & buf & vbLf Loop Close #1
ReadShiftJisFile = tmp
End Function
|
UTF-8で読み込む
Private Function ReadUtf8File(ByVal filePath As String)
With CreateObject("ADODB.Stream") .Charset = "UTF-8" .Open .LoadFromFile filePath tmp = .ReadText .Close End With
ReadUtf8File = tmp
End Function
|
Shift-JISで書き出す
Private Sub WhiteShiftJisFile(dat, ByVal filePath As String)
tmp = Replace(dat, vbCr, "") tmp = Split(tmp, vbLf) Open filePath For Output As #1 For Each s In tmp Print #1, s Next Close #1
End Sub
|
UTF-8で書き出す
Private Sub WhiteUtf8File(dat, ByVal filePath As String)
tmp = Replace(dat, vbCr, "") tmp = Split(tmp, vbLf) Set ads = CreateObject("ADODB.Stream") With ads .Type = adTypeText .Charset = "UTF-8" .Open End With For Each s In tmp ads.WriteText s, adWriteLine Next ads.SaveToFile filePath, adSaveCreateOverWrite ads.Close Set ads = Nothing
End Sub
|
変換
上で定義したReadShiftJisFile
とWhiteUtf8File
を使って変換が行えます.
Sub ShiftJisToUtf8(ByVal inputFilePath As String, ByVal outputFilePath As String)
dat = ReadShiftJisFile(inputFilePath) Call WhiteUtf8File(dat, outputFilePath)
End Sub
|