JAVA伊奥。Java中的InputStream类

InputStream类是所有io类的超类,即表示字节的输入流。它表示字节的输入流。定义InputStream子类的应用程序必须提供方法,返回输入的下一个字节。 调用reset()方法,将流重新定位到最近标记的位置。 InputStream 宣言:

null
public abstract class InputStream
   extends Object
      implements Closeable

建造师:

  • InputStream():单个构造函数

方法: InputStream Class in Java.

  • 标记(): JAVA伊奥。输入流。标记(int arg) 标记输入流的当前位置。它设置读取限制,即在标记位置无效之前可以读取的最大字节数。 语法:
    public void mark(int arg)
    Parameters :
    arg : integer specifying the read limit of the input Stream
    Return : 
    void
  • read(): JAVA伊奥。输入流。读() 从输入流中读取下一个字节的数据。返回的字节值范围为0到255。如果由于到达流的结尾而没有字节可用,则返回值-1。 语法:
    public abstract int read()
    Parameters :
    ------
    Return : 
    Reads next data else, -1 i.e. when end of file is reached.
    Exception :
    ->  IOException : If I/O error occurs.
  • 关闭(): JAVA伊奥。输入流。关闭() 关闭输入流并将与此流关联的系统资源释放到垃圾收集器。 语法:
    public void close()
    Parameters :
    ------
    Return : 
    void
    Exception :
    ->  IOException : If I/O error occurs.
  • read(): JAVA伊奥。输入流。读取(字节[]arg) 读取arg的字节数。从输入流到缓冲区数组arg的长度。read()方法读取的字节返回int。如果len为零,则不读取字节,返回0;否则,将尝试读取至少一个字节。 语法:
    public int read(byte[] arg)
    Parameters :
    arg : array whose number of bytes to be read
    Return : 
     reads number of bytes and return to the buffer else, -1 i.e. when end of file is reached.
    Exception :
    ->  IOException : If I/O error occurs.
    ->  NullPointerException : if arg is null.
  • 重置(): JAVA伊奥。输入流。重置() 由mark()方法调用。它将输入流重新定位到标记的位置。 语法:
    public void reset()
    Parameters :
    ----
    Return : 
    void
    Exception :
    ->  IOException : If I/O error occurs.
  • markSupported(): JAVA伊奥。输入流。markSupported() 方法测试此输入流是否支持标记和重置方法。默认情况下,InputStream的markSupported方法返回false。 语法:
    public boolean markSupported()
    Parameters :
    -------
    Return : 
    true if input stream supports the mark() and reset() method  else,false
  • 跳过(): JAVA伊奥。输入流。跳过(长arg) 跳过和丢弃 阿格 输入流中的字节。 语法:
    public long skip(long arg)
    Parameters :
    arg : no. of bytes to be skipped
    Return : 
    skip bytes.
    Exception :
    ->  IOException : If I/O error occurs.

    解释InputStream类方法的Java程序:

    // Java program illustrating the working of InputStream method
    // mark(), read(), skip()
    // markSupported(), close(), reset()
    import java.io.*;
    public class NewClass
    {
    public static void main(String[] args) throws Exception
    {
    InputStream geek = null ;
    try {
    geek = new FileInputStream( "ABC.txt" );
    // read() method : reading and printing Characters
    // one by one
    System.out.println( "Char : " +( char )geek.read());
    System.out.println( "Char : " +( char )geek.read());
    System.out.println( "Char : " +( char )geek.read());
    // mark() : read limiing the 'geek' input stream
    geek.mark( 0 );
    // skip() : it results in redaing of 'e' in G'e'eeks
    geek.skip( 1 );
    System.out.println( "skip() method comes to play" );
    System.out.println( "mark() method comes to play" );
    System.out.println( "Char : " +( char )geek.read());
    System.out.println( "Char : " +( char )geek.read());
    System.out.println( "Char : " +( char )geek.read());
    boolean check = geek.markSupported();
    if (geek.markSupported())
    {
    // reset() method : repositioning the stram to
    // marked positions.
    geek.reset();
    System.out.println( "reset() invoked" );
    System.out.println( "Char : " +( char )geek.read());
    System.out.println( "Char : " +( char )geek.read());
    }
    else
    System.out.println( "reset() method not supported." );
    System.out.println( "geek.markSupported() supported" +
    " reset() : " +check);
    }
    catch (Exception excpt)
    {
    // in case of I/O error
    excpt.printStackTrace();
    }
    finally
    {
    // releasing the resources back to the
    // GarbageCollector when closes
    if (geek!= null )
    {
    // Use of close() : closing the file
    // and releasing resources
    geek.close();
    }
    }
    }
    }

    
    

    注: 此代码不会在联机IDE上运行,因为此处不存在suc文件。 您可以在系统上运行此代码来检查工作状态。 ABC。txt 代码中使用的文件

    HelloGeeks

    输出:

    Char : H
    Char : e
    Char : l
    skip() method comes to play
    mark() method comes to play
    Char : o
    Char : G
    Char : e
    reset() method not supported.
    geek.markSupported() supported reset() : false

    本文由 莫希特·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

    如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享