首页 > 其他分享 >FileInputStream读取文件的几种方式

FileInputStream读取文件的几种方式

时间:2022-10-18 11:22:23浏览次数:86  
标签:fis 字节 len 几种 file new FileInputStream 读取

三种方式:

1、直接进行读取

//读取文本文件

public static  void textFile()throws IOException{
    // 1 封装文件类
    File file = new File("D://file");
    // 2 将一个字节流这个管,放到源文件上
    FileInputStream fis = new FileInputStream(file);
    // 3 开始读取动作
    int n = fis.read();
    while (n!=-1){
        System.out.println(n);
        n = fis.read();
    }
    // 4 关闭流
    fis.close();
}

2、利用字节类型的缓冲数组

//利用字节类型的缓冲数组

public static void byteReadFile() throws IOException{
    //利用字节流将文件中内容读到程序中来
    // 1 封装文件类
    File file = new File("D://file");
    // 2 将一个字节流这个管,放到源文件上
    FileInputStream fis = new FileInputStream(file);
    // 3 开始读取动作 利用缓冲数组(快递员小车)
    byte[] b = new byte[1024 * 6];
    int len = fis.read(b);
    while (len!=-1){
        for (int i = 0; i < len; i++) {
            System.out.println(b[i]);
        }
        len = fis.read(b);
    }
    // 4 关闭流
    fis.close();
}

3、一个字节一个字节的读取

//一个字节一个字节的读取

public static void oneByteReadFile() throws IOException{
    //利用字节流将文件中内容读到程序中来
    // 1 封装文件类
    File file = new File("D://file");
    // 2 将一个字节流这个管,放到源文件上
    FileInputStream fis = new FileInputStream(file);
    // 3 开始读取动作 定义一个计数器,用来读取计入字节个数
    int count = 0;
    int len = fis.read();
    while (len!=-1){
        count++;
        System.out.println(len);
        len = fis.read();
    }
    System.out.println("count="+count);
    // 4 关闭流
    fis.close();
}

下面是三种一起的比较:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**

  • @Auther: jqq

  • @Date: 2022/10/18 - 10 - 18 - 10:43

  • @Description: PACKAGE_NAME

  • @version: 1.0
    */
    public class FileInputStreamDemon {
    public static void main(String[] args) throws IOException {
    textFile();
    byteReadFile();
    oneByteReadFile();
    }

    //读取文本文件
    public static void textFile()throws IOException{
    // 1 封装文件类
    File file = new File("D://file");
    // 2 将一个字节流这个管,放到源文件上
    FileInputStream fis = new FileInputStream(file);
    // 3 开始读取动作
    int n = fis.read();
    while (n!=-1){
    System.out.println(n);
    n = fis.read();
    }
    // 4 关闭流
    fis.close();
    }

    //利用字节类型的缓冲数组
    public static void byteReadFile() throws IOException{
    //利用字节流将文件中内容读到程序中来
    // 1 封装文件类
    File file = new File("D://file");
    // 2 将一个字节流这个管,放到源文件上
    FileInputStream fis = new FileInputStream(file);
    // 3 开始读取动作 利用缓冲数组(快递员小车)
    byte[] b = new byte[1024 * 6];
    int len = fis.read(b);
    while (len!=-1){
    for (int i = 0; i < len; i++) {
    System.out.println(b[i]);
    }
    len = fis.read(b);
    }
    // 4 关闭流
    fis.close();
    }

    //一个字节一个字节的读取
    public static void oneByteReadFile() throws IOException{
    //利用字节流将文件中内容读到程序中来
    // 1 封装文件类
    File file = new File("D://file");
    // 2 将一个字节流这个管,放到源文件上
    FileInputStream fis = new FileInputStream(file);
    // 3 开始读取动作 定义一个计数器,用来读取计入字节个数
    int count = 0;
    int len = fis.read();
    while (len!=-1){
    count++;
    System.out.println(len);
    len = fis.read();
    }
    System.out.println("count="+count);
    // 4 关闭流
    fis.close();
    }

}

标签:fis,字节,len,几种,file,new,FileInputStream,读取
From: https://www.cnblogs.com/jqqlf/p/16801987.html

相关文章