加载普通 Class 文件
1、首先定义一个测试类
public class SecretUtil {
public String hello(){
return "Hello";
}
}
2、将测试类转换为自定义二进制文件【加密】
//Class文件加密
public class ClassEncoder {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("D:\\com\\roy\\classLoader/SecretUtil.class");
// 将文件后缀 改为 myclass
File targetFile = new File("D:\\com\\roy\\classLoader/SecretUtil.myclass");
if(targetFile.exists()) {
targetFile.delete();
}
FileOutputStream fos = new FileOutputStream(targetFile);
int code = 0;
fos.write(2); // 写为二进制文件时 在最前面加一个数字
while((code = fis.read())!= -1 ) {
fos.write(code);
}
fis.close();
fos.close();
System.out.println("文件转换完成");
}
}