zoukankan      html  css  js  c++  java
  • byte[]和File相互转换

    1.File转byte[]

        /**
         * 文件转byte数组
         * @param file
         * @return
         * @throws IOException
         */
        public static byte[] fileToByte(File file) throws IOException{
            FileInputStream in = new FileInputStream(file);
            BufferedInputStream bf = new BufferedInputStream(in);
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] bytes = new byte[2048000];
            int len = 0;
            while((len=in.read(bytes)) != -1){
                bout.write(bytes,0,len); 
            }
            byte[] fileBytes = bout.toByteArray();
            return fileBytes;
        }

    2.byte[]转File

        /**
         * byte数组转文件
         * @param bytes
         * @return
         * @throws IOException 
         */
        public static File byteToFile(byte[] bytes,String path,String name) throws IOException{
            File dir = new File(path);  
            if(!dir.exists()&&!dir.isDirectory()){//判断文件目录是否存在  
                dir.mkdirs();  
            }
            
            File file = new File(path + "\" + name);
            FileOutputStream fout = new FileOutputStream(file);
            BufferedOutputStream bout = new BufferedOutputStream(fout);
            bout.write(bytes);
            return file;
        }
  • 相关阅读:
    多表查询,连表查询
    mysql数据概念难点
    mysql练习题
    linux下 redis
    nginx安装
    八皇后问题 OpenJ_Bailian
    Prime Ring Problem hdu-1016 DFS
    Oil Deposits hdu-1241 DFS
    Highways
    畅通工程再续
  • 原文地址:https://www.cnblogs.com/dbutil/p/9441097.html
Copyright © 2011-2022 走看看