Java基础进阶——Java IO实例

1611阅读 0评论2012-02-09 qhw
分类:Java

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.     }


上一篇:MySQL索引和优化查询
下一篇:Java基础进阶——“ClassLoader之二——自定义ClassLoader”