ASP에서 FileSystemObject를 이용해서 UTF-8 파일을 읽으면 글자가 깨진다. ASCII나 유니코드(UCS-2) 파일이 아닌 경우는 FileSystemObject로 파일을 읽을 수 없다.
ASP에서 UTF-8 파일을 읽거나 쓸 때 다음 함수를 이용하면 유용한다.
- Function ReadUTF8File(sFileName)
- Dim Stream, TextBuffer
- Set Stream = Server.CreateObject("ADODB.Stream")
- With Stream
- .Charset = "utf-8"
- .Type = 2 'adTypeText
- .Open
- .LoadFromFile sFileName
- .Position = 0
- ReadUTF8File = .ReadText
- .Close
- End With
- Set Stream = Nothing
- End Function
- Function WriteUTF8File(sFileName, sText)
- Dim Stream
- Set Stream = Server.CreateObject("ADODB.Stream")
- With Stream
- .Charset = "utf-8"
- .Type = 2 'adTypeText
- .Open
- .WriteText sText
- .SaveToFile sFileName, 2 'adSaveCreateOverWrite
- .Close
- End With
- Set Stream = Nothing
- End Function