我需要将原始字节写入文件。我这样做:
(.write (FileOutputStream "/path") bytes)
...其中字节必须是 byte[] 类型。请注意,它不能是 Byte[]。
我试图用 (bytes) 和/或 (into-array) 函数来转换我的序列,结果很沮丧,一个例子:
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 的 into-array 工作正常。但是,字节数组没有。文件变空:
(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))))