关于判断一个文件是否是class文件
最近看到一篇文章,其中有一行代码是判断class文件类型的,代码片段:
InputStream is = new InputStream("Test.Class");
DataInputStream dis;
if
(is
instanceof
DataInputStream)
dis = (DataInputStream) is;
else
dis =
new
DataInputStream(is);
if
(dis.readInt() !=
0xcafebabe
)
throw
new
IOException(
"not a .class file"
);
由于对IO操作不是很精通,对这行代码产生的疑问
if
(dis.readInt() !=
0xcafebabe
),
查阅相关资料,readInt()方法是读取输入流的四个字节,而class文件都是以CA FE BA BE 开头的,上图


更多精彩