<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-8  評論-67  文章-0  trackbacks-0
     1package 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}

    91
    posted on 2008-02-01 04:11 獨孤求敗 閱讀(4575) 評論(5)  編輯  收藏 所屬分類: Java ByteCode

    評論:
    # re: 一個讀取Class文件的示例程序 2008-02-01 09:16 | astamei
    這個就是一個讀二進制文件吧?  回復  更多評論
      
    # re: 一個讀取Class文件的示例程序 2008-02-01 12:27 | 獨孤求敗
    @astamei
    是啊,實際上就是將一個二進制文件轉換為十六進制文件
      回復  更多評論
      
    # re: 一個讀取Class文件的示例程序 2008-02-17 12:46 | 93ttu
    好東西呀  回復  更多評論
      
    # re: 一個讀取Class文件的示例程序 2008-03-25 16:53 | 王躍峰
    不知道博主還在寫嗎?我研究了下,寫了程序要獲取到class中的method的參數名稱,目前只能得到方法描述.
    一般是這樣的
    create(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
    我現在想知道參數的名字啊,常量池中有這個名字的
    怎樣通過method_info聯系到哪個參數名稱?
    求老大賜教.mail:wang.yue.feng@163.com  回復  更多評論
      
    # re: 一個讀取Class文件的示例程序 2010-02-16 06:04 | doon
    好!  回復  更多評論
      

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 暖暖日本免费中文字幕| 亚洲人成精品久久久久| 亚洲av午夜福利精品一区人妖| 亚洲国产另类久久久精品黑人 | 免费国产成人高清在线观看麻豆| 国产亚洲sss在线播放| 久久精品a一国产成人免费网站| 国产裸模视频免费区无码| 亚洲色一区二区三区四区| 午夜精品在线免费观看| 在线视频亚洲一区| 1024免费福利永久观看网站| 亚洲无线码在线一区观看| 99在线热播精品免费99热| 亚洲成年轻人电影网站www| 精品成人一区二区三区免费视频 | 毛片视频免费观看| 亚洲视频无码高清在线| 中文字幕在线视频免费| 国产精品美女自在线观看免费| 亚洲AV无码成人精品区日韩| mm1313亚洲精品无码又大又粗| 久久亚洲私人国产精品vA| 亚洲一区二区三区免费观看| 久久精品国产69国产精品亚洲| 久久青草精品38国产免费| 亚洲一区二区三区亚瑟| 精品无码人妻一区二区免费蜜桃| 亚洲av日韩av综合| 四虎在线播放免费永久视频| 亚洲国产乱码最新视频| 免费一级毛片在播放视频| 久久精品成人免费网站| 亚洲日本乱码卡2卡3卡新区| 亚洲不卡AV影片在线播放| 国产好大好硬好爽免费不卡| 久久久国产亚洲精品| 久久久久亚洲AV成人网人人软件| 182tv免费视视频线路一二三| WWW国产亚洲精品久久麻豆| 国产亚洲一区二区精品|