首页 > 其他分享 >IO流详解

IO流详解

时间:2023-02-19 21:44:20浏览次数:32  
标签:字节 详解 IO FileInputStream new 输入 读取

简介

IO是Input和Output的简称,即输入和输出,数据读取到计算机内存中的过程就是输入,内存中的数据输出到外部(如文件和数据库)的过程就是输出。IO 流在 Java 中分为输入流和输出流,而根据数据的处理方式又分为字节流和字符流。

Java IO 流的 40 多个类都是从如下 4 个抽象类基类中派生出来的。

  • InputStream/Reader: 所有的输入流的基类,前者是字节输入流,后者是字符输入流。
  • OutputStream/Writer: 所有输出流的基类,前者是字节输出流,后者是字符输出流。

 

InputStream(字节输入流)

InputStream用于从源头(通常是文件)读取数据(字节信息)到内存中,java.io.InputStream抽象类是所有字节输入流的父类。

 FileInputStream 是一个比较常用的字节输入流对象,可直接指定文件路径,可以直接读取单字节数据,也可以读取至字节数组中。

BufferedInputStream

不过,一般我们是不会直接单独使用 FileInputStream ,通常会配合 BufferedInputStream来进行读取:  
 1  try {
 2             BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream("web-socket-serviceimpl/src/main/resources/application.yml"));
 3             int content=0;
 4             while (content!= -1) {
 5                 content=bufferedInputStream.read();
 6                 System.out.println((char) content);
 7             }
 8         } catch (FileNotFoundException e) {
 9             throw new RuntimeException(e);
10         } catch (IOException e) {
11             throw new RuntimeException(e);
12         }

DataInputStream

DataInputStream 用于读取指定类型数据,不能单独使用,必须结合 FileInputStream 。
1 DataInputStream dataInputStream = new DataInputStream(new FileInputStream("web-socket-serviceimpl/src/main/resources/application.yml"));
2             System.out.println( dataInputStream.readLine());

 

         

标签:字节,详解,IO,FileInputStream,new,输入,读取
From: https://www.cnblogs.com/hx-web/p/17134925.html

相关文章