【发布时间】:2016-06-04 01:17:48
【问题描述】:
以下代码:
(spit "/Users/nha/tmp/spit.txt" (.getBytes "hello"))
生成一个包含“[B@71e054bc”的文件,该文件与内容无关(在本例中为“hello”),因为这是JVM representation of the address of a byte array。
但是以下工作(取自this SO post):
(clojure.java.io/copy
(.getBytes "hello")
(java.io.File. "/Users/nha/tmp/spit.txt"))
然后该文件包含正确的内容“hello”。
为什么spit 会这样?有没有办法扩展它的字节行为?
【问题讨论】:
-
您可以将字节包装在一个字符串中,
(spit "spit.txt" (String. (.getBytes "hello")))?很难看出这有什么用,特别是因为原始字节没有编码并且输出将取决于平台。