In Pharo/Smalltalk: 如何读取具有特定编码的文件?
In Pharo/Smalltalk: How to read a file with a specific encoding?
我目前正在阅读这样的文件:
dir := FileSystem disk workingDirectory.
stream := (dir / 'test.txt' ) readStream.
line := stream nextLine.
这在文件 utf-8
编码时有效,但当文件具有另一种编码时我无法找到该怎么做。
类 ZnCharacterReadStream
和 ZnCharacterWriteStream
提供
使用 UTF-8
(默认)以外的编码字符流的功能。首先,需要将文件流转换为 binary
流。在此之后,它可以被一个ZnCharacter*Stream
包裹起来。这是写入和读取文件的完整示例:
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?'"
有关详细信息,Enterprise Pharo a Web Perspective 书中有一章是关于字符编码的。
对于 Pharo 7,有这个 guide for file streams,建议:
('test.txt' asFileReference)
readStreamEncoded: 'cp-1250' do: [ :stream |
stream upToEnd ].
我目前正在阅读这样的文件:
dir := FileSystem disk workingDirectory.
stream := (dir / 'test.txt' ) readStream.
line := stream nextLine.
这在文件 utf-8
编码时有效,但当文件具有另一种编码时我无法找到该怎么做。
类 ZnCharacterReadStream
和 ZnCharacterWriteStream
提供
使用 UTF-8
(默认)以外的编码字符流的功能。首先,需要将文件流转换为 binary
流。在此之后,它可以被一个ZnCharacter*Stream
包裹起来。这是写入和读取文件的完整示例:
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?'"
有关详细信息,Enterprise Pharo a Web Perspective 书中有一章是关于字符编码的。
对于 Pharo 7,有这个 guide for file streams,建议:
('test.txt' asFileReference)
readStreamEncoded: 'cp-1250' do: [ :stream |
stream upToEnd ].