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

     

  • 相关阅读:
    20190127-将一个文件拆分为多个新文件
    20190125-找到列表第二大的数以及自己写一个冒泡排序
    20190121-n个人围成一圈,凡报到3的人退出圈子,最后留下的是原来第几号的那位
    20190120-自定义实现split方法
    20190118-自定义实现replace方法
    20190118-利用Python实现Pig Latin游戏
    20190116-将特定数字插入一个已经排序好的序列并且不改变其排序规则
    20190112-自定义实现字符串的操作方法,如strip,upper,title,ljust,center,zfill,find,rfind等
    20190110-用笨办法找到二维矩阵的鞍点
    我想转行—程序员转行自媒体
  • 原文地址:https://www.cnblogs.com/gaoquanquan/p/9621451.html
Copyright © 2011-2022 走看看