InputStream

InputStream的作用是标志那些从不同数据起源产生输入的类。这些数据起源包括(每个都有一个相关的InputStream子类)。
基础资料
  • 外文名:InputStream
  • 定义:读取一个字节序列的对象
  • 作用:标志从不同数据起源产生输入的类
  • 领域:Java
  • 表示:InputStream和OutputStream
  • 简介

    java的InputStream的类型

    InputStream的作用是标志那些从不同数据起源产生输入的类。这些数据起源包括(每个都有一个相关的InputStream子类):

    (1) 字节数组

    (2) String对象

    (3) 文件

    (4) “管道”,它的工作原理与现实生活中的管道类似:将一些东西一端置入,它们在另一端输出。

    (5) 一个由其他种类的流组成的序列,以便我们将其统一收集合并到一个流内。

    (6) 其他数据集,如Internet连接等。

    除此以外,FilterInputStream也属于InputStream的一种类型,用它可为“破坏器”类提供一个基础类,以便将属性或者有用的接口同输入流连接到一起。这将在以后讨论。

    Class

    Function

    Constructor Arguments

    How to use it

    ByteArray-InputStream

    Allows a buffer in memory to be used as an InputStream.

    The buffer from which to extract the bytes.

    As a source of data. Connect it to a FilterInputStreamobject to provide a useful interface.

    StringBuffer-InputStream

    Converts a Stringinto an InputStream.

    A String. The underlying implementation actually uses a StringBuffer.

    As a source of data. Connect it to a FilterInputStreamobject to provide a useful interface.

    File-InputStream

    For reading information from a file.

    A Stringrepresenting the file name, or a Fileor FileDescriptorobject.

    As a source of data. Connect it to a FilterInputStreamobject to provide a useful interface.

    类 功能 构建器参数/如何使用

    ByteArrayInputStream 允许内存中的一个缓冲区作为InputStream使用 从中提取字节的缓冲区/作为一个数据源使用。通过将其同一个FilterInputStream对象连接,可提供一个有用的接口

    StringBufferInputStream 将一个String转换成InputStream 一个String(字串)。基础的实施方案实际采用一个StringBuffer(字串缓冲)/作为一个数据源使用。通过将其同一个FilterInputStream对象连接,可提供一个有用的接口

    FileInputStream 用于从文件读取信息 代表文件名的一个String,或者一个File或FileDescriptor对象/作为一个数据源使用。通过将其同一个FilterInputStream对象连接,可提供一个有用的接口

    Piped-InputStream

    Produces the data that’s being written to the associated PipedOutput-Stream. Implements the “piping” concept.

    PipedOutputStream

    As a source of data in multithreading. Connect it to a FilterInputStreamobject to provide a useful interface.

    Sequence-InputStream

    Coverts two or more InputStreamobjects into a single InputStream.

    Two InputStreamobjects or an Enumerationfor a container of InputStreamobjects.

    As a source of data. Connect it to a FilterInputStreamobject to provide a useful interface.

    Filter-InputStream

    Abstract class which is an interface for decorators that provide useful functionality to the other InputStreamclasses. See Table 10-3.

    See Table 10-3.

    See Table 10-3.

    PipedInputString 产生为相关的PipedOutputStream写的数据。实现了“管道化”的概念 PipedOutputStream/作为一个数据源使用。通过将其同一个FilterInputStream对象连接,可提供一个有用的接口

    SequenceInputStream 将两个或更多的InputStream对象转换成单个InputStream使用 两个InputStream对象或者一个Enumeration,用于InputStream对象的一个容器/作为一个数据源使用。通过将其同一个FilterInputStream对象连接,可提供一个有用的接口

    FilterInputStream 对作为破坏器接口使用的类进行抽象;那个破坏器为其他InputStream类提供了有用的功能。参见表10.3

    InputStream的使用方法

    对输入文件作缓冲

    要想打开文件读取字符,你得先用String或File对象创建一个FileInputReader。为了提高速度,你应该对这个文件作缓冲,因此你得把FileInputReader的reference交给BufferedReader。由于BufferedReader也提供了readLine( )方法,因此它就成为你最终要使用的那个对象,而它的接口也成为你使用的接口了。当你读到了文件的末尾时,readLine( )会返回一个null,于是就退出while循环了。

    String s2是用来累加文件内容的(再加一个换行符,因为readLine( )会把它们都剥掉)。后面的程序都要用到这个s2。最后,用close( )来关闭文件。单从技术角度上说,程序退出的时候(不管有没有垃圾要回收)都应该调用finalize( ),而finalize( )又会调用close( )。不过各种JVM的实现并不一致,所以最好还是明确地调用close( )。

    演示了如何用System.in来生成一个能读取控制台输入的流。System.in是一个InputStream,而BufferedReader需要一个Reader作参数,所以要先通过InputStreamReader来转转手。

    读取内存

    现在String s2里面已经有一个完整的文件了。因此这部分程序要用它去创建一个StringReader。然后用(StringReader的)read( )方法把字符读出来,再送到控制台上去。注意,read( )会把读出来的byte当作int,所以要想正常打印的话,你得先把它们转换成char。

    读取格式化的内存

    要想读取"格式化"的数据,你就得用DataInputStream了,它是一个面向byte的I/O类 (不是面向char的),因此你只能从头到底一直用InputStream了。当然你可以把所有东西(比方说文件) 都当成byte,然后用InputStream读出来,但这里是String。要想把String变成成byte数组,可以用String的getBytes( )方法,而ByteArrayInputStream是可以处理byte数组的。到了这一步,你就不用担心没有合适的InputStream来创建DataInputStream了。

    如果你是用readByte( )逐字节地读取DataInputStream的话,那么无论byte的值是多少,都是合法的,所以你无法根据返回值来判断输入是否已经结束了。你只能用available( )来判断还有多少字符。

    读取文件

    这段程序还演示了怎样往文件里面写数据。先创建一个FileWriter。BufferedWriter总是免不掉的。(试试把BufferedWriter去掉,你就能看到它对性能的影响了—— 缓冲能大幅提高I/O的性能)。然后再让PrintWriter去排版。这样就能得出能够读得懂的,普通的文本文件了。

    随着程序一行一行地写文件,我们就顺便加了一个行号。注意,我们没用LineNumberInputStream,因为这是一个傻乎乎的,没什么用的类。正如这里所看到的,要知道行号还不简单。

    输入流用完之后,readLine( )会返回null。你会发现,程序显式地调用了out1的close( ),因为如果写文件的时候不调用close( ),它是不会去清空缓冲区的,这样就有可能会落下一些东西了。

    在JAVA中,InputStream是所有字节输入流的父类。

    首页
    科技
    #贵族
    最新入驻
    贾科莫·普契尼
    Caroline Lufkin
    翁建宇
    相关阅读
    类型系统
    内容词条·755人浏览
    Internet小型计算机系统接口
    内容词条·6379人浏览
    前缀码
    内容词条·1257人浏览
    光盘塔
    内容词条·6696人浏览
    全局钩子
    内容词条·3927人浏览
    CSS2
    内容词条·1158人浏览
    • 网站地图
    • |

    Copyright 2023 fuwu029.com赣ICP备2022008914号-4