- private byte[] loadClassData(String name){
-
FileInputStream fis = null;
-
byte [] data = null;
-
try {
-
fis = new FileInputStream(
-
new File(
-
"E:\\project\\Approve\\bin\\jack\\classloader\\test\\" + name + ".class"));
-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
int ch = 0;
-
while ((ch = fis.read()) != -1) {
-
baos.write(ch);
-
}
-
data = baos.toByteArray();
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
return data;
- }
2.读取远程文件并转换为字符数组
- private byte[] loadClassData(String name){
-
byte [] data = null;
-
try {
-
URL urlRemote = new URL("" + name + ".class");//须保证远程文件地址可达
-
URLConnection uConnection = urlRemote.openConnection();
-
InputStream inputStream = uConnection.getInputStream();
-
int length = uConnection.getContentLength();
-
data = new byte[length];
-
inputStream.read(data);
-
inputStream.close();
-
return data;
-
} catch (Exception e) {
-
System.out.println(name +"类没有找到!");
-
e.printStackTrace();
-
}
-
return data;
- }