2017-03-03 90 views

回答

4

这里是如何

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" 
+0

非常感谢!我想要做的是读取在文本编辑器中输入的数据(名称:ops)并将每行插入到OrderedCollection中。 val:=(ops value)readStream。 [val atEnd] whileFalse: [aCollection add:val nextLine]。 但我得到一个ReadStream不明白的错误。有什么建议么? – MuM6oJuM6o

+0

你用什么方言?什么是你的文本编辑器的类? –

+0

我正在使用cincom IDE.I'm从GUI文本编辑器中获取值。我只是用文本编辑器(ops value class)检查了从文本编辑器中检索的值的类型。他们是一种可以将Text对象转换为String的方式吗? – MuM6oJuM6o