将class文件dump下来以及读取class文件的字节
package com.inspire;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class test9 {
public static void main(String[] args) {
String pathName=outputClazz(new byte[]{1,1,1},"com.inspire.HeloController","D:\\aaa\\bbb\\");
System.out.println(content.length);
}
private static String outputClazz(byte[] bytes,String className,String pathName) {
String[] split = className.split("\\.");
FileOutputStream out = null;
try {
for(int i=0;i<split.length-1;i++){
pathName+=split[i]+"/";
}
File folders =new File(pathName);
boolean exists = folders.exists();
if(!exists){
folders.mkdirs();
}
pathName=pathName+split[split.length-1]+".class";
out = new FileOutputStream(pathName);
out.write(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != out) try {
out.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
return pathName;
}
public static byte[] getContent(String filePath) {
File file = new File(filePath);
long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
System.out.println("file too big...");
return null;
}
FileInputStream fi = null;
byte[] buffer = new byte[(int) fileSize];
try {
fi = new FileInputStream(file);
int offset = 0;
int numRead = 0;
while (true) {
if (!(offset < buffer.length
&& (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0)) break;
offset += numRead;
}
if (offset != buffer.length) {
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return buffer;
}
}