java.io.FileInputStream

    科技2025-10-31  4

    java.io.FileInputStream

    文件字节输入流,万能的,任何类型的文件都可以采用这个流来读。

    字节的方式,完成输入的操作,完成读的操作(硬盘—>内存)

    常用的方法

    int read()

    FileInputStream fis = null; try { //创建文件字节输入流对象 //以下都是采用了:绝对路径 //文件路径:E:\学习\Java\代码相关\基础语法\src\com\IO\temp (IDEA会自动把\变成\\,因为\表示转义) //E:/学习/Java/代码相关/基础语法/src/com/IO/temp 这种方式也是可以的 fis = new FileInputStream("E:\\学习\\Java\\代码相关\\基础语法\\src\\com\\IO" + "\\temp.txt"); /* //开始读 int readDate = fis.read();//这个方法的返回值:读取到的字节本身。 System.out.println(readDate);//97 readDate = fis.read(); System.out.println(readDate);//98 readDate = fis.read(); System.out.println(readDate);//99 readDate = fis.read(); System.out.println(readDate);//100 readDate = fis.read(); System.out.println(readDate);//101 readDate = fis.read(); System.out.println(readDate);//102 //已经读取到文件的末尾了,再读的时候读取不到任何数据,返回-1 readDate = fis.read(); System.out.println(readDate);//-1 */ /*while (true){ int readDate = fis.read(); if (readDate == -1){ break; } System.out.println(readDate); }*/ /*for (int readData = fis.read();readData != -1 ;){ System.out.println(readData); readData = fis.read(); }*/ int readData = 0; while ((readData = fis.read()) != -1){ 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(); } } }

    这种一个字节一个字节读取的方式效率低下 内存和硬盘交互太频繁,基本时间/资源都浪费在交互上面了。

    这种方式不用

    int read(byte[] b) 一次最多读取b.length个字节。

    减少硬盘和内存的交互,提高程序的执行效率。

    往byte[]当中读

    IDEA的默认当前路径是当前工程Project的根

    FileInputStream fis = null; try { //相对路径 //IDEA的默认当前路径是当前工程Project的根 fis = new FileInputStream("temp.txt"); //开始读,使用byte数组,一次读取多个字节。最多读取“数组.length”个字节。 byte[] bytes = new byte[4];//准备一个4个长度的byte数组,一次最多读取4个字节。 /*//这个方法的返回值是:读取到的字节数量。(不是字节本身) int readCount = fis.read(bytes); System.out.println(readCount);//第一次读取到了4个字节 System.out.println(new String(bytes,0,readCount)); readCount = fis.read(bytes); System.out.println(readCount);//第二次读取到了2个字节 System.out.println(new String(bytes,0,readCount)); readCount = fis.read(bytes); System.out.println(readCount);//第三次没有读取到任何字节 返回-1 */ /*while (true){ int readCount = fis.read(bytes); if (readCount == -1) break; System.out.println(new String(bytes,0,readCount)); }*/ //改良的循环 int readCount = 0; 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(); } } }

    int available() 返回流当中剩余的没有读到的字节数量

    FileInputStream fis = null; try { fis = new FileInputStream("temp.txt"); System.out.println("总字节数量" + fis.available()); byte[] bytes = new byte[fis.available()];//这种方法不适合大文件,因为bytes[]数组不能太大 fis.read(bytes); System.out.println(new String(bytes)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } }

    long skip(long n) 跳过几个字节不读

    fis.skip(5);//跳过5个字节不读取,很好理解

    Processed: 0.010, SQL: 8