java类加载文章

(19条消息) 获取class类字节数组的方法_iteye_9747的博客-CSDN博客

 

public static byte[] loadBytes(Class<?> cls) throws IOException {
if (cls == null)
return null;
String name = cls.getCanonicalName().replaceAll("\\.", "/") + ".class";
InputStream is = ClassLoader.getSystemResourceAsStream(name);
BufferedInputStream bis = new BufferedInputStream(is);
try {
int length = is.available();
byte[] bs = new byte[length];
System.err.println("ddd:" + bs.length);
bis.read(bs);
is.close();
return bs;
} finally {
bis.close();
}
}
posted @ 2022-08-05 00:17  dronff  阅读(17)  评论(0)    收藏  举报