将class文件dump下来以及读取class文件的字节

将class文件dump下来以及读取class文件的字节

package com.inspire;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class test9 {
    public static void main(String[] args) {
        String pathName=outputClazz(new byte[]{1,1,1},"com.inspire.HeloController","D:\\aaa\\bbb\\");//注意路径结尾带\
        byte[] content = getContent(pathName);
        System.out.println(content.length);
    }
    //指定好文件夹及文件后,自动创建文件夹和文件,如果文件夹已经创建,则不做任何操作
    private static String outputClazz(byte[] bytes,String className,String pathName) {
        String[] split = className.split("\\.");
        FileOutputStream out = null;
        try {
            for(int i=0;i<split.length-1;i++){
                pathName+=split[i]+"/";
            }
            //创建文件夹
            File folders =new File(pathName);
            boolean exists = folders.exists();
            if(!exists){
                folders.mkdirs();
            }
            pathName=pathName+split[split.length-1]+".class";
            /*
            //如果文件存在,则不覆盖旧的文件
            File file=new File(pathName);
            if(file.exists()){
                return pathName;
            }

             */
            //如果文件存在,则覆盖旧的文件
            out = new FileOutputStream(pathName);
            out.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != out) try {
                out.close();
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return pathName;
    }

    public static byte[] getContent(String filePath) {
        File file = new File(filePath);
        long fileSize = file.length();
        if (fileSize > Integer.MAX_VALUE) {
            System.out.println("file too big...");
            return null;
        }
        FileInputStream fi = null;
        byte[] buffer = new byte[(int) fileSize];
        try {
            fi = new FileInputStream(file);
            int offset = 0;
            int numRead = 0;
            while (true) {
                if (!(offset < buffer.length
                        && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0)) break;

                offset += numRead;
            }
            if (offset != buffer.length) {
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fi.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 确保所有数据均被读取
        return buffer;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Inspiration666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值