zoukankan      html  css  js  c++  java
  • java——io、字节流缓冲区拷贝文件、字节缓冲流

    使用try catch finally关闭文件流:

      写入文件:

    import java.io.*;
    public class exp{
        public static void main(String[] args) {
            //流要在try外面声明,不然finally里面找不到这个流
            OutputStream file = null;
            try{
                file = new FileOutputStream("iooooo.txt");
                String str = "北邮
    ";
                byte[] b = str.getBytes();
                for (int i=0; i<b.length; i++){
                    file.write(b[i]);
                }
            //在这里,如果发生异常,则close()不能被执行,所以要在finally里close()
            //file.close();
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                try{
                    if(file!=null)
                        file.close();
                    }catch(Exception e){
                        e.printStackTrace();
                    }
            }
            
        }
    }

      读文件:

    import java.io.*;
    public class exp{
        public static void main(String[] args) {
            //流要在try外面声明,不然finally里面找不到这个流
            InputStream file = null;
            try{
                file = new FileInputStream("ioo.txt");
                int b = 0;
                while(true){
                    b = file.read();
                    if(b == -1){
                        break;
                    }
                    System.out.println(b);
                }
            //在这里,如果发生异常,则close()不能被执行,所以要在finally里close()
            //file.close();
            }catch(Exception e){
                System.out.println("try");
            }finally{
                try{
                    if(file!=null)
                        file.close();
                        System.out.println(file);
                    }catch(Exception e){
                        System.out.println("finally");
                    }
            }
            
        }
    }

    字节流缓冲区拷贝文件:

    import java.io.*;
    public class exp{
        public static void main(String[] args) throws Exception {
            InputStream in = new FileInputStream("pic.png");
            OutputStream out = new FileOutputStream("pp.png");
            byte[] buff = new byte[1024];
            int len;
            while((len=in.read(buff)) != -1){
                out.write(buff, 0, len);
            }
            in.close();
            out.close();
        }
    }

    在写这个的时候我没有仔细研究b=in.read()和b=in.read(byte[])的区别,以至于没弄懂这个buff。下面简单说明一下:

    第一种:

    int len;
    while((len=in.read()) != -1){
        out.write(len);
    }

    上面这种情况下in.read()返回的是从输入流中读取的8个字节的数据,并以int形式保存在len中。out.write(len);接受int中保存的数据,并将其写入out流中。

    第二种:

    byte[] buff = new byte[1024];
    int len;
    while((len=in.read(buff)) != -1){
        out.write(buff, 0, len);
    }

    这种情况下,len=in.read(buff);中len表示的是读取字节的数目,此例就是1024,而真正存储数据的变成了buff这个字节数组。out.write(buff, 0, len);接受buff中从0到len个字节的数据,并将其写入out流中。

    bingo~

    字节缓冲流:存储很快~

    import java.io.*;
    public class exp{
        public static void main(String[] args) throws Exception {
            BufferedInputStream bis = new BufferedInputStream(
                new FileInputStream("pic.png"));
            BufferedOutputStream  bos = new BufferedOutputStream(
                new FileOutputStream("pp.png"));
            int b;
            while((b=bis.read()) != -1){
                bos.write(b);
            }
            bis.close();
            bos.close();
        }
    }

     

  • 相关阅读:
    Jmeter索引贴
    Jenkins Jmeter Tomcat Html实施方案
    转载:几种方法来实现scp拷贝时无需输入密码
    Jenkins Jmeter Ant Email Html实施方案
    zabbix3.4 zabbix-server zabbix-agent zabbix-java-gateway安装 配置
    shell () {}
    Vuser中常用的C语言函数
    loadrunner C语言循环for while do...while
    loadrunner C语言switch case
    linux系统扫描技术及安全防范--------待补全
  • 原文地址:https://www.cnblogs.com/gaoquanquan/p/9621451.html
Copyright © 2011-2022 走看看