我需要将原始字节写入文件。我这样做是:
(.write (FileOutputStream "/path") bytes)
...其中字节必须是 byte[] 类型。请注意它不能是Byte[]。
我尝试使用(字节)和/或(数组)函数转换我的序列,但很沮丧,例如:
user=> (bytes (into-array (filter #(not (= % 13)) (to-byte-array (File. "e:/vpn.bat")))))
java.lang.ClassCastException: [Ljava.lang.Byte; cannot be cast to [B (NO_SOURCE_FILE:0)
继续:
带有 Byte/TYPE 的数组工作正常。然而,字节数组却没有。文件变空:
(import 'FileOutputStream)
(use 'clojure.contrib.io)
(defn remove-cr-from-file [file]
(with-open [out (FileOutputStream. file)]
(let [dirty-bytes (to-byte-array file)
clean-seq (filter #(not (= 13 %)) dirty-bytes)
clean-bytes (byte-array clean-seq)]
(.write out clean-bytes))))
最佳答案
还有字节数组函数。
如果需要打包字节数组,您可以查看 http://github.com/geoffsalmon/bytebuffer或者直接使用底层的 Java ByteBuffer 东西。
关于clojure - 如何在 Clojure 中将序列转换为 byte[]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3450954/