FileInputStream 初入

FileInputStream 初入

文本:temp(没有任何格式)
文本内容:abcde

代码:

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

public class FileInputStreamTest01 {
    public static void main(String[] args) {

        // 创建文件字节输入流对象
        // 路径:C:\Users\yao\Desktop\temp

        FileInputStream fis = null;

        try {
            // 两种类型的斜杠都可以
//            FileInputStream file = new FileInputStream("C:\\Users\\yao\\Desktop\\temp");   可以
            fis = new FileInputStream("C:/Users/yao/Desktop/temp");   //也可以

            // 读
            // read 方法返回值:读取到的 “字节” 本身
            int readData1 = fis.read(); // 这里选择细粒度的异常控制方式
            System.out.println(readData1);  // 97 (a)

            int readData2 = fis.read();
            System.out.println(readData2);  // 98 (b)

            int readData3 = fis.read();
            System.out.println(readData3);  // 99 (c)

            int readData4 = fis.read();
            System.out.println(readData4);  // 100 (d)

            int readData5 = fis.read();
            System.out.println(readData5);  // 101 (e)

            int readData6 = fis.read();
            System.out.println(readData6);  // -1


        } catch (FileNotFoundException e) {
            e.printStackTrace();
            // 在此确保流一定会关闭
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流前提:流非空。流是 null 时没必要关,也是避免空指针异常
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

运行结果

97
98
99
100
101
-1

使用 while 读取:

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

public class FileInputStreamTest02 {
    public static void main(String[] args) {

        FileInputStream fis = null;

        try {
            fis = new FileInputStream("C:/Users/yao/Desktop/temp");

            // 读取的升级版
            while (true){
                int data = fis.read();
                if (data == -1){
                    break;
                }
                System.out.println(data);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
97
98
99
100
101

再次改进 while 循环:

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

public class FileInputStreamTest02 {
    public static void main(String[] args) {

        FileInputStream fis = null;

        try {
            fis = new FileInputStream("C:/Users/yao/Desktop/temp");

            // 不写成死循环
            int data = 0;
            while((data = fis.read()) != -1){
                System.out.println(data);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

这里会得到同样的结果

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值