【问题标题】:How to write txt file in smalltalk如何在smalltalk中写入txt文件
【发布时间】:2020-04-21 15:16:04
【问题描述】:

我尝试使用此代码:

f := 'testfile.txt' asFileReference.
f2 := f writeStream.
f2 nextPutAll: 'hello world'.
f2 close.
f content.

但我得到了这个例外:

**FileDoesNotExistException**

【问题讨论】:

标签: smalltalk


【解决方案1】:

补充一下 Estaban 的回应,Pharo 的一个令人惊讶的行为是 writeStreamDo 会覆盖现有文件,因此如果现有文件比新数据长,您最终会得到新数据和旧数据的尾部.幸运的是,有一个简单的解决方案:您可以简单地包含 truncate。所以稍微“更安全”的版本是:

    'testfile.txt' asFileReference 
writeStreamDo: [ :stream | stream truncate. stream << 'Hello, World!' ].

【讨论】:

    【解决方案2】:
    'testfile.txt' asFileReference 
        writeStreamDo: [ :stream | stream << 'Hello, World!' ].
    

    这应该可行。但这是表达你之前所做的另一种方式,所以我怀疑某些写作许可是错误的或与之相关的。

    【讨论】:

      猜你喜欢
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多