FileInputStream读取字节流。读取文件数据的两种方式(写的好)

总结:   

//1读取文件的数据到字节流inputStream
    InputStream inputStream = new FileInputStream("D:\\demo.txt");//读取文件的数据到字节流inputStream。

//2将字节流inputStream转换成字符流inputStreamReader
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);//将字节流inputStream转换成字符流inputStreamReader。
  
//3读取字符流inputStreamReader的数据到字符数组ch中,并返回字符数组ch的真实的字符数
    char []ch = new char[1024];
    int len = isr.read(ch);

转载:https://blog.csdn.net/a909301740/article/details/52574602

FileInputStream(文件字节读取流):

read():一个一个字节的读

read(byte[] buf):先把字节存入到缓冲区字节数组中,一下读一个数组(常用)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
 
public class FileInputStreamDemo1 {
 
    private static final int SIZE = 4096;
 
    public static void main(String[] args) throws IOException {
        
        /*
         * 将已有文件的数据读取出来
         * 既然是读,使用InputStream
         * 而且是要操作文件。FileInputStream
         * 
         */
        
        //为了确保文件一定在之前是存在的,将字符串路径封装成File对象
        File file = new File("tempfile\\fos.txt");
        if(!file.exists()){
            throw new RuntimeException("要读取的文件不存在");
        }
        
        //创建文件字节读取流对象时,必须明确与之关联的数据源。
        FileInputStream fis = new FileInputStream(file);
        
        //调用读取流对象的读取方法
        //1.read()返回的是读取到的字节
        //2.read(byte[] b)返回的是读取到的字节个数
        
        //1. 
//        int by=0;
//        while((by=fis.read())!=-1){
//            System.out.println(by);
//        }
        
        //2.
//        byte[] buf = new byte[3];
//        int len = fis.read(buf);//len记录的是往字节数组里存储的字节个数
//        System.out.println(len+"...."+Arrays.toString(buf));//只是转成了字符串的表现形式
//        System.out.println(len+"...."+new String(buf,0,len));//转成字符串
//        
//        int len1 = fis.read(buf);
//        System.out.println(len1+"...."+new String(buf,0,len1));
        
        //创建一个字节数组,定义len记录长度
        int len = 0;
        byte[] buf = new byte[SIZE];
        while((len=fis.read(buf))!=-1){
            System.out.println(new String(buf,0,len));
        }
        
        //关资源
        fis.close();
    }
 
}

下面的写的也还不错

转载:https://blog.csdn.net/omg_c/article/details/52038302

 参数 in对象通过 InputStream in = System.in;获得。//读取键盘上的数据。 
    或者 InputStream in = new FileInputStream(String fileName);//读取文件中的数据。可以看出 FileInputStream 为InputStream的子类。 
主要方法 :int read();//读取单个字符。 
                 int read(char []cbuf);//将读取到的字符存到数组中。返回读取的字符数。

扫描二维码关注公众号,回复: 4003851 查看本文章

   //1读取文件的数据到字节流inputStream
    InputStream inputStream = new FileInputStream("D:\\demo.txt");//读取文件的数据到字节流inputStream。

   //2将字节流inputStream转换成字符流inputStreamReader
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);//将字节流inputStream转换成字符流inputStreamReader。
  
    //3读取字符流inputStreamReader的数据到字符数组ch中,并返回字符数组ch的真实的字符数
    char []ch = new char[1024];
    int len = isr.read(ch);
 

import java.io.*;
class InputStreamReaderDemo {
  public static void transReadNoBuf() throws IOException {
    /**
     * 没有缓冲区,只能使用read()方法。
     */
    //读取字节流
    //InputStream in = System.in;//读取键盘的输入。
    InputStream in = new FileInputStream("D:\\demo.txt");//读取文件的数据。
    //将字节流向字符流的转换。要启用从字节到字符的有效转换,
    //可以提前从底层流读取更多的字节.
    InputStreamReader isr = new InputStreamReader(in);//读取
    //综合到一句。
    //InputStreamReader isr = new InputStreamReader(
    //new FileInputStream("D:\\demo.txt"));
      
    char []cha = new char[1024];
    int len = isr.read(cha);
    //按照我的理解是:读取字符流ISR的数据到字符数组cha中,并返回字符数组cha的真实的字符数
    System.out.println(new String(cha,0,len));
    isr.close();
 
  }
  public static void transReadByBuf() throws IOException {
    /**
     * 使用缓冲区 可以使用缓冲区对象的 read() 和  readLine()方法。
     */
    //读取字节流
    //InputStream in = System.in;//读取键盘上的数据
    InputStream in = new FileInputStream("D:\\demo.txt");//读取文件上的数据。
    //将字节流向字符流的转换。
    InputStreamReader isr = new InputStreamReader(in);//读取
    //创建字符流缓冲区
    BufferedReader bufr = new BufferedReader(isr);//缓冲
    //BufferedReader bufr = new BufferedReader(
    //new InputStreamReader(new FileInputStream("D:\\demo.txt")));可以综合到一句。
      /*int ch =0;
    ch = bufr.read();
    System.out.println((char)ch);
    */
    String line;
    while((line = bufr.readLine())!=null){
      System.out.println(line);
    }
    isr.close();
  }
}

猜你喜欢

转载自blog.csdn.net/Grace_1203/article/details/83068345