在Clojure中拖拽文件? [英] Tailing a file in Clojure?

查看:103
本文介绍了在Clojure中拖拽文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Clojure中拖放文件的最好方法是什么?我没有遇到任何有助于这样做的实用程序,但是关于如何构建一个的实用程序将非常感激。

What would be the best method to tail a file in Clojure? I haven't come across any utilities that would help do so, but ideas on how to build one would be appreciated!

谢谢。

推荐答案

kotarak 所述,您可以使用 RandomAccessFile 搜索文件结尾。不幸的是,你必须忙等待/睡眠的变化。

As kotarak stated, you could use RandomAccessFile to seek to the end of the File. Unfortunately you have to busy-wait/sleep for changes.

使用延迟序列,您可以处理即时行:

Using a lazy sequence you can process the lines "on-the-fly":

(import java.io.RandomAccessFile)

(defn raf-seq
  [#^RandomAccessFile raf]
  (if-let [line (.readLine raf)]
    (lazy-seq (cons line (raf-seq raf)))
    (do (Thread/sleep 1000)
        (recur raf))))

(defn tail-seq [input]
  (let [raf (RandomAccessFile. input "r")]
    (.seek raf (.length raf))
    (raf-seq raf)))

; Read the next 10 lines
(take 10 (tail-seq "/var/log/mail.log"))

更新:

tail -f /var/log/mail.log -n 0 em> doseq ,因此更改实际上已消耗。

Something like tail -f /var/log/mail.log -n 0, using doseq, so the changes are actually consumed.

(doseq [line (tail-seq "/var/log/mail.log")] (println line))

这篇关于在Clojure中拖拽文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆