zoukankan      html  css  js  c++  java
  • JAVA流操作

    JAVA流操作

    file 转 数组,方法一:

            File file = new File("D:\111.pdf");
            // File 转数组
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            byte[] bytes = bos.toByteArray();
            System.out.println(bytes);
            bos.close();

    file 转 数组,方法二:

            File file = new File("D:\111.pdf");
            FileInputStream fis = new FileInputStream(file);
            byte[] bytes = IoUtil.readBytes(fis, true);
    字节流转文件,方法一:
        /**
         * 将字节流转换成文件
         * @param filename
         * @param data
         * @throws Exception
         */
        public static void saveFile(String filename,byte [] data)throws Exception{
            if(data != null){
                String filepath ="D:\" + filename;
                File file  = new File(filepath);
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(data,0,data.length);
                fos.flush();
                fos.close();
            }
        }

    字节流转文件,方法二:

            File file = new File("D:\111.pdf");
            FileInputStream fis = new FileInputStream(file);
            byte[] bytes = IoUtil.readBytes(fis, true);
            FileOutputStream fos = new FileOutputStream("D://333.PDF");
            IoUtil.write(fos, true, bytes);


  • 相关阅读:
    这个三月不太美丽
    于宇鸿燕百年好合(帮客户名字作诗,祝新婚快乐)
    桃花好运盼君来
    浊水解渴
    聂晶好美(帮客户名字作诗)
    杨晓芳(帮客户名字作诗)
    有你就好
    重回洛带
    我想和你在一起
    再登长城
  • 原文地址:https://www.cnblogs.com/tangshengwei/p/14216166.html
Copyright © 2011-2022 走看看