file - 逐行读取大文件

标签 file clojure io iterator iteration

我正在尝试根据 Clojure 中的迭代编写大文件阅读器。但是如何在 Clojure 中逐行返回字符串呢?我想做这样的东西:

(println (do_something(readFile (:file opts))) ; process and print first line
(println (do_something(readFile (:file opts))) ; process and print second line

代码:

(ns testapp.core
  (:gen-class)
  (:require [clojure.tools.cli :refer [cli]])
  (:require [clojure.java.io]))


(defn readFile [file, cnt]
  ; Iterate over opened file (read line by line)
  (with-open [rdr (clojure.java.io/reader file)]
    (let [seq (line-seq rdr)]
      ; how return only one line there? and after, when needed, take next line?
    )))

(defn -main [& args]
  ; Main function for project 
  (let [[opts args banner] 
        (cli args
          ["-h" "--help" "Print this help" :default false :flag true]
          ["-f" "--file" "REQUIRED: File with data"]
          ["-c" "--clusters" "Count of clusters" :default 3]
          ["-g" "--hamming" "Use Hamming algorithm"]
          ["-e" "--evklid" "Use Evklid algorithm"]
          )]
    ; Print help, when no typed args
    (when (:help opts)
      (println banner)
      (System/exit 0))
    ; Or process args and start work
    (if (and (:file opts) (or (:hamming opts) (:evklid opts)))
      (do
        ; Use Hamming algorithm
        (if (:hamming opts)
          (do
            (println (readFile (:file opts))
            (println (readFile (:file opts))
          )
          ;(count (readFile (:file opts)))
        ; Use Evklid algorithm
        (println "Evklid")))
      (println "Please, type path for file and algorithm!")))) 

最佳答案

可能我不明白“逐行返回”是什么意思,但我建议你编写函数,它接受文件和处理函数,然后打印每一行的处理函数的结果你的大文件。或者,甚至更通用的方式,让我们接受处理函数和输出函数(默认为 println ),所以如果我们不仅想要打印,还想要通过网络发送它,保存在某个地方,发送到另一个线程等:

(defn process-file-by-lines
  "Process file reading it line-by-line"
  ([file]
   (process-file-by-lines file identity))
  ([file process-fn]
   (process-file-by-lines file process-fn println))
  ([file process-fn output-fn]
   (with-open [rdr (clojure.java.io/reader file)]
     (doseq [line (line-seq rdr)]
       (output-fn
         (process-fn line))))))

所以

(process-file-by-lines "/tmp/tmp.txt") ;; Will just print file line by ine
(process-file-by-lines "/tmp/tmp.txt"
                       reverse) ;; Will print each line reversed

关于file - 逐行读取大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25948813/

相关文章:

java - Java写.properties文件时程序在 '\'前加 ':'

objective-c - cocoa 使用关联的应用程序打开多个文件

linux - 如何使用 Red Hat Linux 上的标准工具随机化文件中的行?

php - 浏览另一台服务器 PHP 文件夹中的文件

clojure - 是否有一个适配器可以使函数忽略多余的参数?

sql - Clojure:读取数据库的元数据

c - 关闭(0)后是否​​可以打开标准输入?

linux - 在 bash 中读取/dev/input/eventX

java - 使用 Java 8 处理和拆分大文件

clojure - Clojure 中以撇号结尾的那些数学函数是什么?