• File和byte[]转换


    http://blog.csdn.net/commonslok/article/details/9493531

    public static byte[] File2byte(String filePath)
        {
            byte[] buffer = null;
            try
            {
                File file = new File(filePath);
                FileInputStream fis = new FileInputStream(file);
                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 void byte2File(byte[] buf, 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 + File.separator + fileName);
                fos = new FileOutputStream(file);
                bos = new BufferedOutputStream(fos);
                bos.write(buf);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                if (bos != null)
                {
                    try
                    {
                        bos.close();
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
                if (fos != null)
                {
                    try
                    {
                        fos.close();
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
  • 相关阅读:
    python_24_test
    python_23_tuple
    python_22_enumerate
    python_20_列表
    python_21_copy
    python_19_编码解码
    python_18_三元运算
    python_16_自己建立模块
    关于主键(PRIMARY KEY)和自增(AUTO_INCREMENT)结合使用的知识点
    MySQL root用户忘记密码怎么办?修改密码方法:skip-grant-tables
  • 原文地址:https://www.cnblogs.com/donaldlee2008/p/5844288.html
Copyright © 2020-2023  润新知