In Pharo/Smalltalk: How to read a file with a specific encoding?

时间:2018-03-26 13:34:18

标签: smalltalk pharo

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.

2 个答案:

答案 0 :(得分:3)

班级ZnCharacterWriteStreamUTF-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 ].