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

 

原文地址:https://www.cnblogs.com/gaoquanquan/p/9621451.html