我一直在尝试开发一个小的库来读取Clojure中的MPQ文件,我决定使用RandomAccessFile,因为MPQ是二进制格式.
但是,我在使用方法RandomAccessFile#read()时遇到了麻烦,因为我不确定自己是否正确调用了它
这是代码:
(ns parser
(:import (java.io RandomAccessFile)))
(with-open [file (RandomAccessFile. "replay.SC2Replay" "r")]
(let [x (byte-array 16)]
(.read file [x 0 16])))
当我运行代码时,我得到了
Exception in thread "main" java.lang.ClassCastException: clojure.lang.PersistentVector cannot be cast to [B (parser.clj:0)
at clojure.lang.Compiler.eval(Compiler.java:5440)
at clojure.lang.Compiler.load(Compiler.java:5857)
at clojure.lang.Compiler.loadFile(Compiler.java:5820)
at clojure.main$load_script.invoke(main.clj:221)
at clojure.main$script_opt.invoke(main.clj:273)
at clojure.main$main.doInvoke(main.clj:354)
at clojure.lang.RestFn.invoke(RestFn.java:409)
at clojure.lang.Var.invoke(Var.java:365)
at clojure.lang.AFn.applyToHelper(AFn.java:163)
at clojure.lang.Var.applyTo(Var.java:482)
at clojure.main.main(main.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.ClassCastException: clojure.lang.PersistentVector cannot be cast to [B
at parser$eval7.invoke(parser.clj:7)
at clojure.lang.Compiler.eval(Compiler.java:5424)
... 15 more
例外.我也尝试对临时变量执行此操作
(let [x []]
但它们都产生相同的异常. Java中的相同代码将是这样的
RandomAccessFile file = new RandomAccessFile("replay.SC2Replay", "r");
byte[] x;
file.read(x, 0, 16);
解决方法:
您正在传递单个向量作为参数,它试图将其转换为字节数组,以便它可以调用采用字节数组的单个参数read方法.只需按原样传递参数即可.
(with-open [file (RandomAccessFile. "replay.SC2Replay" "r")]
(let [x (byte-array 16)]
(.read file x 0 16)))