The classes ZnCharacterReadStream
and ZnCharacterWriteStream
provide
functionality to work with encoded character streams other then UTF-8
(which is the default). First, the file stream needs to be converted into a binary
stream. After this, it can be wrapped by a ZnCharacter*Stream
. Here is a full example for writing and reading a file:
dir := FileSystem disk workingDirectory.
(dir / 'test.txt') writeStreamDo: [ :out |
encoded := ZnCharacterWriteStream on: (out binary) encoding: 'cp1252'.
encoded nextPutAll: 'Über?'.
].
content := '?'.
(dir / 'test.txt') readStreamDo: [ :in |
decoded := ZnCharacterReadStream on: (in binary) encoding: 'cp1252'.
content := decoded nextLine.
].
content. " -> should evaluate to 'Über?'"
For more details, the book Enterprise Pharo a Web Perspective has a chapter about character encoding.
#encoding:
. – Eke