• 转转转--Java File和byte数据之间的转换


    package cn.iworker.file;  
      
    import java.io.BufferedOutputStream;  
    import java.io.ByteArrayOutputStream;  
    import java.io.File;  
    import java.io.FileInputStream;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
      
    public class FileTest  
    {  
          
        public static void main(String[] args)  
        {  
            file2BetyArray();  
            fileToBetyArray();  
            BetyToFile("D:\Study\Java\First.class");  
        }  
      
        public static byte[] file2BetyArray()  
        {  
            File file = new File("D:\Study\Java\First.class");  
            if (!file.exists()) {  
                return null;  
            }  
            FileInputStream stream = null;  
            ByteArrayOutputStream out = null;  
            try {  
                stream = new FileInputStream(file);  
                out = new ByteArrayOutputStream(1000);  
                byte[] b = new byte[1000];  
                int n;  
                while ((n = stream.read(b)) != -1) {  
                    out.write(b, 0, n);  
                }  
                return out.toByteArray();// 此方法大文件OutOfMemory  
            } catch (Exception e) {  
                System.out.println(e.toString());  
            } finally {  
                try {  
                    stream.close();  
                    out.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
      
            }  
            return null;  
        }  
      
        public static byte[] fileToBetyArray()  
        {  
            FileInputStream fileInputStream = null;  
            File file = new File("D:\Study\Java\First.class");  
            byte[] bFile = null;  
            try {  
                bFile = new byte[(int) file.length()];  
                fileInputStream = new FileInputStream(file);  
                fileInputStream.read(bFile);  
                fileInputStream.close();  
                for (int i = 0; i < bFile.length; i++) {  
                    System.out.print((char) bFile[i]);  
                }  
                System.out.println("Done");  
            } catch (Exception e) {  
                e.printStackTrace();  
            } finally {  
                try {  
                    fileInputStream.close();  
                    bFile.clone();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
            return bFile;  
        }  
      
        public  static File BetyToFile( String filePath)  
        {  
            File file = new File(filePath);  
            BufferedOutputStream stream = null;  
            FileOutputStream fstream = null;  
            byte[] data=new byte[(int)file.length()];  
            try {  
                fstream = new FileOutputStream(file);  
                stream = new BufferedOutputStream(fstream);  
                stream.write(data);  
            } catch (Exception e) {  
                e.printStackTrace();  
            } finally {  
                try {  
                    if (stream != null) {  
                        stream.close();  
                    }  
                    if (null != fstream) {  
                        fstream.close();  
                    }  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
            return file;  
        }  
      
    }  
    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();  
                    }  
                }  
            }  
        }  
  • 相关阅读:
    centos7修改网卡名称
    xtrabackup备份恢复过程
    centos 设置删除提示
    cetos6 安装samba共享文件夹
    centos 安装cacti监控
    centos6 搭建hdwiki
    centos6.8安装superctl 后台管理工具
    centos6.5 安装ansible,管理多台服务器
    CreateParams作用及重写
    c# 获取当前应用程序的路径
  • 原文地址:https://www.cnblogs.com/tenWood/p/7518190.html
Copyright © 2020-2023  润新知