zoukankan      html  css  js  c++  java
  • [JAVA]字节数组流

    import java.io.*;
    
    public class ByteArrayStream {
        public static void main(String[] args) {
            byte[] datas = fileToByteArray("D:/test/1111.mp4");
            byteArrayToFile(datas, "D:/test/byteArrayNew.mp4");
        }
    
        public static byte[] fileToByteArray(String filePath) {
            File src = new File(filePath);
            byte[] dest = null;
            ByteArrayOutputStream baos = null;
            InputStream is = null;
            try {
                is = new FileInputStream(src);
                baos = new ByteArrayOutputStream();
                byte[] buf = new byte[1024 * 10];
                int len = -1;
                while ((len = is.read(buf)) != -1) {
                    baos.write(buf, 0, len);
                }
                baos.flush();
    
                return baos.toByteArray();
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
            return null;
        }
    
        public static void byteArrayToFile(byte[] src, String filePath) {
            File dest = new File(filePath);
            InputStream is = null;
            OutputStream os = null;
            try {
                is = new ByteArrayInputStream(src);
                os = new FileOutputStream(dest);
                byte[] buf = new byte[1024];
                int len = -1;
                while((len = is.read(buf))!=-1){
                    os.write(buf,0,len);
                }
                os.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
        }
    }
  • 相关阅读:
    sudo详解
    Linux按照CPU、内存、磁盘IO、网络性能监测
    基于kickstart定制自动化安装的linux镜像系统
    基于busybox和LFS的linux系统定制
    syslog及syslog-ng详解 日志服务器
    yum报错
    二进制程序及其依赖库的移植脚本
    Linux 系统裁剪
    linux 内核手动编译
    linux rc.sysinit文件详解
  • 原文地址:https://www.cnblogs.com/zhengxl5566/p/10361571.html
Copyright © 2011-2022 走看看