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);


  • 相关阅读:
    蓝桥杯基础练习题4(16进制转8进制)2
    实验三
    文法
    用文法描述词法规则
    词法分析程序的功能
    个人对于编译原理的一些看法的。
    完成登录与注册
    javaScript基础
    css实例
    导航,头部,CSS基础
  • 原文地址:https://www.cnblogs.com/tangshengwei/p/14216166.html
Copyright © 2011-2022 走看看