Smalltalk:将输出写入文件

发布于 2024-09-29 15:12:21 字数 86 浏览 5 评论 0原文

通常,我将输出写入到成绩单中...

Transcript show:

如何将输出写入文件?

Usually with my output I am writing it to the Transcript with...

Transcript show:

How does one write the output to a file instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

廻憶裏菂餘溫 2024-10-06 15:12:21

您想要使用 FileStream

请参阅此描述 FileStreams 的链接

摘录如下:


FileStream
FileStreams support all of ExternalStreams protocol. They can be created to read, write, readWrite or append from/to a file.
Creation:

* 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

You want to use a FileStream

See this link describing FileStreams

Excerpt below:


FileStream
FileStreams support all of ExternalStreams protocol. They can be created to read, write, readWrite or append from/to a file.
Creation:

* 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

The above was the internal low level instance creation protocol, which is somewhat politically incorrect to use. For portability, please use the companion class Filename to create 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
别念他 2024-10-06 15:12:21
| 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.
| 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.
空名 2024-10-06 15:12:21

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

And then of course don't forget to handle the character encoding you want, if you're not writing binary or the default encoding. In Pharo/Squeak, set the converter to the needed TextConverter subclass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文