一个读取Class文件的示例程序

package bytecodeResearch;
 2
 3import java.io.BufferedInputStream;
 4import java.io.BufferedWriter;
 5import java.io.FileInputStream;
 6import java.io.FileWriter;
 7import java.io.IOException;
 8
 9public class ReadAndWriteClass {
10    
11    //16进制数字字符集 
12    private static String hexString = "0123456789ABCDEF"
13
14    /**
15     * 将字节数组的指定长度部分编码成16进制数字字符串
16     * @param  buffer 待编码的字节数组
17     * @param  length 指定的长度
18     * @return  编码后连接而成的字符串
19     */
20    public static String encode(byte[] buffer,int length) 
21    {     
22        StringBuilder sbr = new StringBuilder();
23        //将字节数组中每个字节拆解成2位16进制整数 
24        for(int i=0;i<length;i++
25        
26            sbr.append(hexString.charAt((buffer[i]&0xf0)>>4)); 
27            sbr.append(hexString.charAt(buffer[i]&0x0f)); 
28            sbr.append("  ");
29        } 
30        return sbr.toString(); 
31    } 
32    
33    /**
34     * 读取一个Class文件,将其所有字节转换为16进制整数,并以字符形式输出
35     * @param  inputPath  输入文件的完整路径
36     * @param  outputPath 输出文件的完整路径
37     * @throws IOException 读写过程中可能抛出的异常
38     */
39    public static void rwclass(String inputPath, String outputPath) throws IOException
40    {
41        //读取Class文件要用字节输入流
42        BufferedInputStream bis = new BufferedInputStream(
43                new FileInputStream(inputPath));
44        //输出转换后的文件要用字符输出流
45        BufferedWriter bw = new BufferedWriter(
46                new FileWriter(outputPath));
47        
48        int readSize = 16;
49        byte[] buffer_read = new byte[readSize];
50        String line;
51        String lineNumber = "0000000";
52        String strReplace;
53        int i = 0;
54        while ((readSize = bis.read(buffer_read,0,readSize))!= -1)
55        {
56            line = encode(buffer_read,readSize);
57            strReplace = Integer.toHexString(i);
58            lineNumber = lineNumber.substring(07-strReplace.length());
59            lineNumber = lineNumber+strReplace;
60            line = lineNumber+"0h: "+line;
61            bw.write(line);
62            bw.newLine();
63            i++;
64        }
65        bis.close();
66        bw.close();
67    }
68
69    /**
70     * 程序的入口方法
71     * @param args
72     * @throws IOException
73     */
74    public static void main(String[] args)
75    {
76        //指定输入、输出文件的完整路径
77        String inputPath = "L:/HelloWorld/HelloWorld.class";
78        String outputPath = "L:/HelloWorld/HelloWorld_ByteCode.txt";
79        
80        try {
81            rwclass(inputPath, outputPath);    
82            System.out.println("Successfully !");
83        } catch (IOException ioe) {
84            System.err.println("Something wrong with reading or writing !");
85            ioe.printStackTrace();            
86        }    
87        
88    }
89
90}

转载于:https://my.oschina.net/sniperLi/blog/353419

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值