在Clojure 1.3中,如何读写一个文件 [英] In Clojure 1.3, How to read and write a file
问题描述
我想知道在clojure 1.3中阅读和编写文件的推荐方式。
- 如何阅读
- 如何读取文件
- 如何编写新文件
- How to read the whole file
- How to read a file line by line
- How to write a new file
- How to add a line to an existing file
假设我们只在这里执行文本文件而不是一些疯狂的二进制文件。
数字1:如何读取整个文件到内存中。 b $ b
(slurp/tmp/test.txt)
当它是一个非常大的文件时,不推荐使用。
第2行:如何逐行读取文件 / p>
(使用'clojure.java.io)
(with-open [rdr(reader/ tmp / test。 txt)]
(doseq [line(line-seq rdr)]
(println line)))
$ b b
with-open
宏关心读者在body的结尾处关闭。阅读器函数强制字符串(它也可以做一个URL等)到 BufferedReader
。 line-seq
提供一个延迟序列。要求将延迟seq结果的下一个元素转换为从阅读器读取的行。
第3个:如何写入新文件
(使用'clojure.java.io)
(with-open [wrtr test.txt)]
(.write wrtr要写入的行))
再次, with-open
注意 BufferedWriter
在正文结尾处关闭。 Writer将一个字符串强制转换为一个 BufferedWriter
,你使用通过java interop:(。write wrtrsomething)。
您也可以使用 spit
,与 slurp
:
(spit/tmp/test.txt要写入的行)
第4项:在现有文件中附加一行。
(使用'clojure.java.io)
(with-open [wrtr(writer/tmp/test.txt:append true)]
(.write wrtr要附加的行))
with append option。
或再次使用 spit
,与 slurp
:
(spit/tmp/test.txt要写入的行:append true)
PS:要更明确地说明您正在阅读和写作到文件而不是其他文件,您可以先创建一个File对象,然后将其强制为 BufferedReader
或作者:
(reader(file/tmp/test.txt))
;;或
(作者(文件tmp / test.txt))
文件功能
PS2:有时,它可以方便地查看当前目录)是。您可以通过两种方式获取绝对路径: (System / getPropertyuser.dir)
或
>(java.io.File。。).getAbsolutePath)
I'd like to know the "recommended" way of reading and writing a file in clojure 1.3 .
解决方案 Assuming we're only doing text files here and not some crazy binary stuff.
Number 1: how to read an entire file into memory.
(slurp "/tmp/test.txt")
Not recommended when it is a really big file.
Number 2: how to read a file line by line.
(use 'clojure.java.io)
(with-open [rdr (reader "/tmp/test.txt")]
(doseq [line (line-seq rdr)]
(println line)))
The with-open
macro takes care that the reader is closed at the end of the body. The reader function coerces a string (it can also do a URL, etc) into a BufferedReader
. line-seq
delivers a lazy seq. Demanding the next element of the lazy seq results into a line being read from the reader.
Number 3: how to write to a new file.
(use 'clojure.java.io)
(with-open [wrtr (writer "/tmp/test.txt")]
(.write wrtr "Line to be written"))
Again, with-open
takes care that the BufferedWriter
is closed at the end of the body. Writer coerces a string into a BufferedWriter
, that you use use via java interop: (.write wrtr "something").
You could also use spit
, the opposite of slurp
:
(spit "/tmp/test.txt" "Line to be written")
Number 4: append a line to an existing file.
(use 'clojure.java.io)
(with-open [wrtr (writer "/tmp/test.txt" :append true)]
(.write wrtr "Line to be appended"))
Same as above, but now with append option.
Or again with spit
, the opposite of slurp
:
(spit "/tmp/test.txt" "Line to be written" :append true)
PS: To be more explicit about the fact that you are reading and writing to a File and not something else, you could first create a File object and then coerce it into a BufferedReader
or Writer:
(reader (file "/tmp/test.txt"))
;; or
(writer (file "tmp/test.txt"))
The file function is also in clojure.java.io.
PS2: Sometimes it's handy to be able to see what the current directory (so ".") is. You can get the absolute path in two ways:
(System/getProperty "user.dir")
or
(-> (java.io.File. ".") .getAbsolutePath)
这篇关于在Clojure 1.3中,如何读写一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!