package tools;

import java.io.*;

public class filetobytes {
    public static byte[] Filetobyte(File tradeFile){
        byte[] buffer = null;
        try
        {
            FileInputStream fis = new FileInputStream(tradeFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return buffer;
    }
    public static File getFile(byte[] bfile, String filePath,String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if(!dir.exists()||!dir.isDirectory()){//判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(filePath+"\\"+fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bfile);
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return file;
    }

}
最近下载更多
annazhang  LV29 2023年4月8日
Tuxxxxx  LV3 2023年1月3日
高金磊abc  LV12 2022年3月18日
TerryGaoBo  LV9 2021年8月16日
wjh12345654321  LV14 2021年7月27日
zhaojialiang  LV7 2021年5月14日
85489354756a  LV3 2021年3月6日
3969138  LV15 2020年11月11日
ehewhee  LV6 2020年9月11日
六弄咖啡馆  LV4 2020年6月6日
最近浏览更多
Shengyi188 2023年12月18日
暂无贡献等级
xwpvip 2023年10月26日
暂无贡献等级
annazhang  LV29 2023年4月7日
lilonghui  LV3 2023年3月12日
Tuxxxxx  LV3 2023年1月3日
xmr1003  LV1 2022年8月8日
adcxiaoxueren 2022年4月17日
暂无贡献等级
King_wsk  LV18 2022年3月24日
张三12138 2022年1月5日
暂无贡献等级
fengqianqian  LV9 2021年12月15日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友