FileInputStream

java.io.FileInputStream

  • 文件字节输入流,万能的,任何类型的文件都可以采用这个流来读。
  • 字节的方式,完成输入的操作,完成读的操作(从硬盘—>到内存
主要方法:
  • read():从输入流中读取一个数据字节
  • read(byte[] b):从输入流中将最多b.length个字节的数据读入一个byte数组中。
  • read(byte[] b, int ,off, int len):从输入流中将最多len个字节的数据读入一个byte数组中。
read():
import java.io.FileInputStream;

public class FileInputStreamTest01 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {  //main方法中抛出异常一般使用try...catch
            // 创建文件字节输入流对象
            fis = new FileInputStream("D:/javaIO");
            
            //读取流
            
            int readData = fis.read();  //read()返回下一个数据字节;如果已到达文件末尾,则返回-1
            System.out.println(readData);
            
            //再次调用
            readData = fis.read();
            System.out.println(readData);  //返回下一个字节
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //在finally语句块当中确保流一定关闭
            if (fis != null) {  //避免空指针异常
                //关闭流的前提是:流不是空的,流是null时没必要关闭
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

优化代码:

import java.io.FileInputStream;

public class FileInputStreamTest02 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("D:/javaIO");
            while(true) {
                int readData = fis.read();
                if(readData == -1) {  //如果为-1跳出循环
                    break;
                }
                System.out.println(readData); 
            }
            
            //改造while循环
            int readData = 0;
            while((readData = fis.read()) != -1) {
                System.out.println(readData); 
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
read(byte[] b):

(tempfile为项目下子建的文件里面有”abcdef“6个字节)

/*
int read(byte[] b)
   一次最多读取b.length个字节。
   减少硬盘和内存的交互,提高程序的执行效率
   往byte[]数组当中读
*/

public class FileInputStreamTest03 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            //相对路经是从当前所在的位置作为起点开始找
            fis = new FileInputStream("tempfile"); //tempfile文件就在该项目的根
            
            //开始读,采用byte数组,一次读取多个字节。最多读取“数组.length”个字节
      //如果文件中有6个字节数:123456
            byte[] bytes = new byte[4];  //准备一个4个长度的byte数组,一次最多读取4个字节。
            
            //这个方法的返回值是:读取到的字节数量。(不是字节本身)
            int readCount = fis.read(bytes);
            System.out.println(readCount); //第一次读取到了4个字节,返回4
            //将字节数组全部转换成字符串:写法有问题
            System.out.println(new String(bytes));  // 1234
            System.out.println(new String(bytes,0,readCount)); //正确写法
            
            readCount = fis.read(bytes);
            System.out.println(readCount); //第二次读取到了2个字节,返回2
            System.out.println(new String(bytes));  // 5634,56把12顶替
            System.out.println(new String(bytes,0,readCount)); //正确写法
            
            readCount = fis.read(bytes);
            System.out.println(readCount); //第三次1个也没读取到返回-1
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

优化后的代码:

public calss FileInputStreamTest04 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("tempfile");
            //准备一个bytes数组长度
            byte[] bytes = new byte[4];
           /* while(true) {
                int readCount = fis.read(bytes);
                if(readCount == -1) {
                    break;
                }
                //把byte数组转换成字符串,读到多少个转换多少个
                System.out.print(new String(bytes, 0, readCount));
            }*/
            //while循环转变
            int readCount = 0;
            while ((readCount = fis.read(bytes)) != -1) {
                System.out.print(new String(bytes , 0 , readCount))
            }
        } catch (FileNOtFountException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
available()和skip(long n)方法:
  • available:返回流当中剩余的没有读到的字节数量
  • skip:跳过几个字节不读。

available():

public calss FileInputStreamTest05 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("tempfile");
            //读取1个字节
            int readByte = fis.read();
            //还剩下可以读的字节数量是:5
            System.out.println("剩下多少个字节没有读:" + fis.available());  //返回没有读取的字节个数
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

skip():

public calss FileInputStreamTest05 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("tempfile");
            System.out.println("粽子节数量:" + fis.available());
            
            fis.skip(3);  //跳过三个字节
            System.out.println(fis.read());  //返回100
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值