zoukankan      html  css  js  c++  java
  • Java File 与 Bytes相互转换

    public static byte[] fileToBytes(String filePath) {
            byte[] buffer = null;
            File file = new File(filePath);
            
            FileInputStream fis = null;
            ByteArrayOutputStream bos = null;
    
            try {
                fis = new FileInputStream(file);
                bos = new ByteArrayOutputStream();
    
                byte[] b = new byte[1024];
    
                int n;
    
                while ((n = fis.read(b)) != -1) {
                    bos.write(b, 0, n);
                }
                
                buffer = bos.toByteArray();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(JmsReceiver.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(JmsReceiver.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    if (null != bos) {
                        bos.close();
                    }
                } catch (IOException ex) {
                    Logger.getLogger(JmsReceiver.class.getName()).log(Level.SEVERE, null, ex);
                } finally{
                    try {
                        if(null!=fis){
                            fis.close();
                        }
                    } catch (IOException ex) {
                        Logger.getLogger(JmsReceiver.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            
            return buffer;
        }
    public static void bytesToFile(byte[] buffer, final String filePath){
    
            File file = new File(filePath);
    
            OutputStream output = null;
            BufferedOutputStream bufferedOutput = null;
    
            try {
                output = new FileOutputStream(file);
    
                bufferedOutput = new BufferedOutputStream(output);
    
                bufferedOutput.write(buffer);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally{
                if(null!=bufferedOutput){
                    try {
                        bufferedOutput.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                if(null != output){
                    try {
                        output.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
    
        }
  • 相关阅读:
    05-java学习-循环结构
    04-java学习-选择结构
    03-java学习-基本数据类型-运算符-键盘接收用户输入
    A02-java学习-classpath配置-标识符-java变量类型
    A01-java学习环境准备
    20190215面试-C#操作外设-多线程-shocket
    装饰者模式
    状态模式
    DllImport学习
    网络编程(一)----基础知识、数据流套接字
  • 原文地址:https://www.cnblogs.com/yshyee/p/6609285.html
Copyright © 2011-2022 走看看