1
是否有一种方法可以在Smalltalk中逐行读取输入行? 我发现了一种方法,即使用“upTo:Character cr”。 有没有其他方法? 或者我可以读取行作为字符串?从输入中读取数据在Smalltalk中逐行写入流
在此先感谢。
是否有一种方法可以在Smalltalk中逐行读取输入行? 我发现了一种方法,即使用“upTo:Character cr”。 有没有其他方法? 或者我可以读取行作为字符串?从输入中读取数据在Smalltalk中逐行写入流
在此先感谢。
这里是如何
string := 'line one
line two
line three'.
stream := string readStream
现在,
stream nextLine "answers with 'line one'".
stream nextLine "answers with 'line two'".
stream nextLine "answers with 'line three'"
,并在这一点上
stream atEnd "answers with true"
注意nextLine
消耗结束行,而不包括它的答案。如果最后一行没有行尾,则nextLine
将在最后停止。
另请注意,这允许循环读取线而stream
有更多的数据
[stream atEnd] whileFalse: [self doSomethingWith: stream nextLine]
如果你想从头阅读:
stream reset
,如果你想回到前面的位置:
stream position: pos
例如
stream nextLine "read first line".
pos2 := stream position "position of the second line".
stream nextLine "read second line".
stream nextLine "read third line".
stream position: pos2 "get back to line 2".
stream nextLine "again, line 2"
非常感谢!我想要做的是读取在文本编辑器中输入的数据(名称:ops)并将每行插入到OrderedCollection中。 val:=(ops value)readStream。 [val atEnd] whileFalse: [aCollection add:val nextLine]。 但我得到一个ReadStream不明白的错误。有什么建议么? – MuM6oJuM6o
你用什么方言?什么是你的文本编辑器的类? –
我正在使用cincom IDE.I'm从GUI文本编辑器中获取值。我只是用文本编辑器(ops value class)检查了从文本编辑器中检索的值的类型。他们是一种可以将Text对象转换为String的方式吗? – MuM6oJuM6o