java-io流

io流

流:数据从源点传输到汇点的“管道”

java中流的分类:

​ 按照方向分(永远站在(当前正在写的)程序的角度分析): 输入流 输出流

​ 按照单位分: 字节流 字符流

​ 按照功能分: 节点流 过滤流(不能直接连数据的源点和汇点的)、包装流、处理流

InputStream 所有字节输入流统一的父类 抽象类

​ int read( ):

​ int read(byte[ ] date):

​ int read(byte[ ] date,int off,int len):

OutPutStream 所有字节输出流统一的父类 抽象类

​ write(int date):

​ write(byte[ ] date):

​ write(byte[ ] date,int off,int len):

FileInputStream: 输入流 字节流 节点流

FileOutputStream: 输出流 字节流 节点流

*:他们都是节点流 构造方法允许传入File对象或者String路径

*:尽管他们都是节点流 但是只能连接文件 不能连接目录 否则直接出现异常

*:FileOutputStream是节点输出流 节点输出流创建对象的时候

​ 如果其连接的文件不存在 他会自动把文件创建处理 不需要手动创建 其实在File类当中 有个方法叫createNewFile( )

​ 如果其连接的目录结构都不存在 则直接出现异常 不能帮我们创建 所在在File类当中 有个方法叫mkdirs( )

*:FileOutputStream 是节点输出流 它在创建对象的时候

​ 如果其连接的文件已经存在 也会在创建流的那一刻被新的空白文件替换

​ 如果我们的需求是想要在现有文件内容的后面 追加新的内容 而不是替换掉他 则可以通过构造方法传参 开启追加模式

​ new FileOutputStream(“abc.txt”,true);//true = >追加模式开启

*:FileInputStream最常用的是read(byte[ ] data)无参的read( )效率很低

*:FileOutputStream 最常用的却是write(byte[ ],int,int)不会导致“烂尾巴”

*:FileInputStream都是以-1作为读取结束的标识的

*:我们要学会标准的try catch 处理异常和 TWR 处理异常

*:在用完流的第一时间 及时的关闭流 (TWR是不需要写close( )的)

import java.io.*;
public class test{
    public static void main(String[] args)throws Exception{
        FileInputStream fis = new FileInputStream("abc.txt");
        /*
        //不是不行  而是不好  太慢了
        int data ;
        while((date == fis.read()) != -1){
            System.out.println((char)data):
        }
        */
        byte[] data = new byte[3];
        while(len = fis.read(data) != -1){
            for(int i = 0;i < len;i++){
            	System.out.println((char)data[i]);
            }
        }
        fis.close();
    }
}

组合拳:

import java.io.*;
public class test{
    public static void main(String[] args)throws Exception{
        FileInputStream fis = new FileInputStream("1.jpg");
        FileOutputStream fos = new FileOutputStream("3.jpg");
        /*
        //不是不行  而是不好  太慢了
        int data;
        while((data = fis.read()) != -1){
            fos.write(data);
        }
        */
        byte[] data = new byte[3<<20];//3*1024*1024 = 3<10<10 = 3 << 20
        int len;
        while((len = fis.read(data)) != -1){
            fos.write(data,0,len);
        }
        fis.close();
        fos.close();
    }
}

不继承直接抛出异常

import java.io.*;
public class test{
    public static void main(String[] args){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try{
            FileInputStream fis = new FileInputStream("1.jpg");
            FileOutputStream fos = new FileOutputStream("3.jpg");
            /*
            //不是不行  而是不好  太慢了
            int data;
            while((data = fis.read()) != -1){
                fos.write(data);
            }
            */
            byte[] data = new byte[3<<20];//3*1024*1024 = 3<10<10 = 3 << 20
            int len;
            while((len = fis.read(data)) != -1){
                fos.write(data,0,len);
        	}
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                fis.close();
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                try{
                    fos.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
     
    }
}

TWR 是JDK7.0提供是新特性 Try with Resources

import java.io.*;
public class test{
    public static void main(String[] args){
        try(FileInputStream fis = new FileInputStream("1.mp3");FileOutputStream fos = new FileOutputStream("3.mp3")){//结束时不需要close()释放了,自动释放
            byte[] data = new byte[3<<20];
            int len;
            while((len = fis.read(data)) != -1){
                fos.write(data,o,len);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

BufferedInputStream 字节流 输入流 过滤流

BufferedOutputStream 字节流 输出流 过滤流

*:他们都是过滤流 不能直接连接文件 只能连接其他的流

*:他们构造方法的第二个参数 都允许指定缓冲空间的大小 默认8192 = 8K

​ new BufferedInputStream(fis,5<<20);//5m

*:使用BufferedInputStream最常用的 int read( )

*:使用BufferedOutputStream最常用的是write(int data)

*:BufferedInputStream同样以-1作为读取结束的标识

*:BufferedOutputStream是带缓冲的输出流 使用带缓冲的输出流 必须注意 及时清空缓冲 防止数据滞留缓冲区 导致数据丢失

​ 什么情况下会清空缓冲区呢:

					1. 缓冲区满了会自动清空   不需要操作
					2. 关闭流的操作  会触发清空缓冲的操作   close( );
					3. 主动情况缓冲:flush( );
import java.io.*;
public class test{
    public static void main(String[] args)throws Exception{
        FileInputStream fis = new FileInputStream("1.mp3");//针头
        BufferedInputStream bis = new BufferedInputStream(fis);//针管
        
        FileInputStream fos = new FileOutputStream("3.mp3");//针头
        BufferedOutputStream bos = new BufferedOutPutStream(fos);//针管
        
        int data;
        while((data = bis.read()) != -1){
            bos.write(data);
        }
        bis.close();
        //bos.close();
        bos.flush();//将缓冲区当中数据  释放到文件当中
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值