Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2304720
  • 博文数量: 266
  • 博客积分: 5485
  • 博客等级: 大校
  • 技术积分: 3695
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-20 11:05
个人简介

多读书,多做事,广交朋友,趣味丛生

文章分类

全部博文(266)

分类: Java

2012-02-09 08:30:18

1.读取本地文件并转换为字符数组
  
  1. private byte[] loadClassData(String name){
  2.         FileInputStream fis = null;
  3.         byte [] data = null;
  4.         try {
  5.             fis = new FileInputStream(
  6.                 new File(
  7.                     "E:\\project\\Approve\\bin\\jack\\classloader\\test\\" + name + ".class"));
  8.             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  9.             int ch = 0;
  10.             while ((ch = fis.read()) != -1) {
  11.                 baos.write(ch);
  12.             }
  13.             data = baos.toByteArray();
  14.         } catch (Exception e) {
  15.             e.printStackTrace();
  16.         }
  17.         return data;
  18.     }

2.读取远程文件并转换为字符数组
  1. private byte[] loadClassData(String name){
  2.         byte [] data = null;
  3.         try {
  4.             URL urlRemote = new URL("" + name + ".class");//须保证远程文件地址可达
  5.             URLConnection uConnection = urlRemote.openConnection();
  6.             InputStream inputStream = uConnection.getInputStream();
  7.             int length = uConnection.getContentLength();
  8.          data = new byte[length];
  9.          inputStream.read(data);
  10.          inputStream.close();
  11.          return data;
  12.         } catch (Exception e) {
  13.             System.out.println(name +"类没有找到!");
  14.             e.printStackTrace();
  15.         }
  16.         return data;
  17.     }


阅读(1619) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~