FileInputStream初步

FileInputStream

package com.lichennan.io;

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

/*
java.io.FileInputStream:
 1.文件字节输入流,万能的,任何类型的文件都可以采用这个流来读。
 2.字节的方式,完成输入的操作,完成读的操作(硬盘---》内存)
 3.
 */
public class FileInputStreamTest01 {
    public static void main(String[] args) {
        FileInputStream fis = null;
       //创建文件字节输入流对象
        //文件路径D:\测试\temp
        try {
            fis = new FileInputStream("D:\\测试\\temp");
            try {
                //开始读
              int readDate = 0;
               while ((readDate = fis.read())!= -1){
                    System.out.println(readDate);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            //在finally语句块当中确保流一定关闭。
            if (fis != null) {//避免空指针异常
                //关闭流的前提是:流不为空
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46554776/article/details/106239963