Smalltalk:将输出写入文件

时间:2010-10-29 19:46:34

标签: file-io smalltalk

通常在我的输出中,我将它写在Transcript中......

Transcript show:

如何将输出写入文件?

3 个答案:

答案 0 :(得分:7)

您想使用FileStream

请参阅this link describing FileStreams

摘录如下:

<小时/> 的FileStream FileStreams支持所有ExternalStreams协议。可以创建它们以从文件读取,写入,读取写入或附加到文件。 创建:

* for reading:

      aStream := FileStream readonlyFileNamed:aFilenameString

* to read/write an existing file:

      aStream := FileStream oldFileNamed:aFilenameString

* to create a new file for writing:

      aStream := FileStream newFileNamed:aFilenameString

以上是内部低级实例创建协议,在使用上有些政治上不正确。为了便于携带,请使用配套类Filename来创建fileStreams:

* for reading:

      aStream := aFilenameString asFilename readStream

* to read/write an existing file:

      aStream := aFilenameString asFilename readWriteStream

* to create a new file for writing:

      aStream := aFilenameString asFilename writeStream

* to append to an existing file:

      aStream := aFilenameString asFilename appendingWriteStream

答案 1 :(得分:2)

| fileName aStream |

fileName := (Filename named: 'stream.st').

aStream := fileName readAppendStream.

aStream nextPutAll: 'What is the best class I have ever taken?'.

aStream cr.

aStream flush.

aStream nextPutAll: 'It is the VisualWorks Intro class!'.

aStream close.

答案 2 :(得分:0)

然后当然不要忘记处理你想要的字符编码,如果你不是写二进制或默认编码。在Pharo / Squeak中,将转换器设置为所需的TextConverter子类。