你很幸运,我不久前写了一个这样的实用程序。见https://github.com/bjourne/playground-factor/wiki/Tips-and-Tricks-Filesystem#tailing-a-file
USING: accessors io io.encodings.utf8 io.files io.monitors kernel namespaces ;
IN: examples.files.tail
: emit-changes ( monitor -- )
dup next-change drop
input-stream get output-stream get stream-copy* flush
emit-changes ;
: seek-input-end ( -- )
0 seek-end input-stream get stream>> stream-seek ;
: tail-file ( fname -- )
[
dup f <monitor> swap utf8 [
seek-input-end emit-changes
] with-file-reader
] with-monitors ;
我认为您的问题是给出的报价with-input-stream*
将隐式关闭流(each-line
这样做)。我不知道这是否是一个错误。像这样的词可以用来读取完整的流而不关闭它:
: my-stream-contents* ( stream -- seq )
[ [ stream-read1 dup ] curry [ ] ] [ stream-exemplar produce-as nip ] bi ;
然后:
IN: scratchpad "/tmp/foo" utf8 <file-reader> [ my-stream-contents* print ] keep
file contents here
...
--- Data stack:
T{ decoder f ~input-port~ utf8 f }
IN: scratchpad my-stream-contents* print
more file contents here
...