Java基础进阶IO流-FileInputStream字节输入流

导读:本篇文章讲解 Java基础进阶IO流-FileInputStream字节输入流,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

java.io.FileInputStream:

  • (1)文件字节输入流,万能的,任何类型的文件都可以采用这个流来读
  • (2)字节的方式,完成输入的操作,完成读的操作(硬盘–>内存)

示例代码01:

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

        FileInputStream fis = null;
        //创建字节输出流对象
        // 创建文件字节输入流对象
        // 文件路径:D:\course\JavaProjects\02-JavaSE\temp (IDEA会自动把\编程\\,因为java中\表示转义)
        // 以下都是采用了:绝对路径的方式。
        //FileInputStream fis = new FileInputStream("D:\\course\\JavaProjects\\02-JavaSE\\temp");
        // 写成这个/也是可以的。
        try {
            fis = new FileInputStream("D:\\temp.txt");

            //读取流
            int read = fis.read();
            System.out.println(read);

            read = fis.read();
            System.out.println(read);

            read = fis.read();
            System.out.println(read);

            read = fis.read();
            System.out.println(read);

            read = fis.read();
            System.out.println(read);

            read = fis.read();
            System.out.println(read);

            // 已经读到文件的末尾了,再读的时候读取不到任何数据,返回-1.
            read = fis.read();
            System.out.println(read);

            read = fis.read();
            System.out.println(read);

            read = fis.read();
            System.out.println(read);

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

    }
}

在这里插入图片描述

运行结果:

在这里插入图片描述
此处运行结果多出两个数字可能是文件中的数据采用了中文输入法导致

示例代码02:

/*
对第一个程序进行改进。循环方式。
*/
public class FileInputStreamTest02 {
    public static void main(String[] args) {

        FileInputStream fis = null;
        try {
            fis = new FileInputStream("D:/temp.txt");

            //循环读数据
            /*//第一种方式
            while(true){
                int readData = fis.read();
                if(readData == -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();
                }
            }
        }
    }
}

在这里插入图片描述

运行结果:

在这里插入图片描述

示例代码03:

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

        FileInputStream fis = null;
        try {
            // 相对路径的话呢?相对路径一定是从当前所在的位置作为起点开始找!
            // IDEA默认的当前路径是哪里?工程Project的根就是IDEA的默认当前路径。
            //fis = new FileInputStream("temp.txt");//相对路径,工程下
            //fis = new FileInputStream("IO/temp2.txt");//相对路径,IO项目下
            //fis = new FileInputStream("IO/src/temp3.txt");//相对路径,src下
            fis = new FileInputStream("IO/src/com/newstudy/javase/io/temp4.txt");//相对路径,包下

            byte[] bytes = new byte[4];//new一个byte数组,一次可以读四个字节
            int readCount = fis.read(bytes);//读四个abcd
            System.out.println(readCount);//4
            //把读到的字节转换成字符串
            System.out.println(new String(bytes,0,readCount));//读几个转几个

            readCount = fis.read(bytes);//读2个efcd
            System.out.println(readCount);//4
            System.out.println(new String(bytes,0,readCount));//读几个转几个

            readCount = fis.read(bytes);//读0个没数据,1个字节都没有读取到返回-1
            System.out.println(readCount);//-1
            //System.out.println(new String(bytes,0,readCount));//读几个转几个//读不了,那就转换不了,报错异常
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

运行结果:

在这里插入图片描述

完整的字节数输出流

//最终版,字节输出流
public class FileInputStreamTets04 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("IO/src/temp3.txt");

            //循环读文件
            byte[] bytes = new byte[4];//准备byte数组
            /*while(true){
                int readCount = fis.read(bytes);
                if(readCount == -1){
                    break;
                }
                System.out.print(new String(bytes,0,readCount));
            }*/

            //加强版循环读文件
            int readCount = 0;
            /*while((readCount = fis.read(bytes)) != -1){
                System.out.print(new String(bytes));
            }*/

            //读取HelloWorld程序
            while((readCount = fis.read(bytes)) != -1){
                System.out.print(new String(bytes,0,readCount));
            }

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

在这里插入图片描述

运行结果:

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/94259.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!