FIleInputStream循环读

本文介绍了如何使用FileInputStream高效读取文件,强调了使用available()方法获取剩余字节数和skip()方法跳过指定字节在循环读取中的作用,同时指出单字节读取的低效率问题。通过示例展示了相对路径下文件的读取操作。
摘要由CSDN通过智能技术生成

FIleInputStream中方法

int available();返回流当中剩余的没有读到的字节数量
long skip(long n);跳过几个字节不读

一个字节的读取效率慢。硬盘和内存交互太频繁,浪费资源。(推荐下面)

package com;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class test {
   
    public static void main(String[] args) {
   
        FileInputStream fis = null;
        try {
   
            fis = new FileInputStream("D:\\test.txt");

            //死循环去读
          /*  while (true) {
                int read = fis.read();
                if (read == -1) {
                    break;
                }
                System.out.println(read);
            }*/

            //改造while
            int readData=0;
            while ((readData = fis.read())!=-1){
   
                System.out.println(readData);
            }
        } catch (FileNotFoundException e) {
   
            e.printStackTrace();
        } catch (IOException e) {
   
            e.printStackTrace();
        } finally {
   //关闭流
            //关闭流的前提是:流不是空,流是null没必要关闭
            if (fis != null) {
   //避免空指针异常
                t
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值