I am currently reading a file like this:
dir := FileSystem disk workingDirectory.
stream := (dir / 'test.txt' ) readStream.
line := stream nextLine.
This works when the file is utf-8
encoded but I could not find out what to do when the file has another encoding.
答案 0 :(得分:3)
班级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?'"
包裹。以下是编写和读取文件的完整示例:
df[which(df$value2 == min(df$value2)),]
有关详细信息,本书Enterprise Pharo a Web Perspective有一章介绍字符编码。
答案 1 :(得分:1)
对于Pharo 7,有一个guide for file streams,它建议:
('test.txt' asFileReference)
readStreamEncoded: 'cp-1250' do: [ :stream |
stream upToEnd ].