Java中的Java.io.InputStream类用法指南和示例

2021年3月25日12:51:36 发表评论 763 次浏览

InputStream类是所有io类的超类, 即代表字节的输入流。它代表输入的字节流。定义InputStream子类的应用程序必须提供方法, 返回输入的下一个字节。

调用reset()方法, 该方法将流重新定位到最近标记的位置。

输入流

声明 :

public abstract class InputStream
   extends Object
      implements Closeable

构造函数:

  • InputStream():单个构造方法

方法:

Java中的InputStream类。

mark():

Java.io.InputStream.mark(int arg)

标记输入流的当前位置。它设置了readlimit, 即在标记位置变为无效之前可以读取的最大字节数。

句法 :

public void mark(int arg)
Parameters :
arg : integer specifying the read limit of the input Stream
Return : 
void

read():

java.io.InputStream.read()

从输入流中读取下一个数据字节。返回值字节, 范围为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.

close() :

java.io.InputStream.close()

关闭输入流, 并将与此流关联的系统资源释放到垃圾收集器。

句法 :

public void close()
Parameters :
------
Return : 
void
Exception :
->  IOException : If I/O error occurs.

read():

Java.io.InputStream.read(byte [] arg)

从输入流读取arg.length的字节数到缓冲区数组arg。 read()方法读取的字节以int返回。如果len为零, 则不读取任何字节, 并返回0;否则, 返回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.

reset() :

Java.io.InputStream.reset()

由mark()方法调用。它将输入流重新定位到标记的位置。

句法 :

public void reset()
Parameters :
----
Return : 
void
Exception :
->  IOException : If I/O error occurs.

markSupported():

Java.io.InputStream.markSupported()

方法测试此输入流是否支持mark和reset方法。 InputStream的markSupported方法默认情况下返回false。

句法 :

public boolean markSupported()
Parameters :
-------
Return : 
true if input stream supports the mark() and reset() method  else, false

skip():

Java.io.InputStream.skip(long arg)

跳过并丢弃

输入流中的字节数。

句法 :

public long skip(long arg)
Parameters :
arg : no. of bytes to be skipped
Return : 
skip bytes.
Exception :
->  IOException : If I/O error occurs.

Java程序解释InputStream类方法:

// 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();
             }
         }
     }
}

注意 :

由于此处没有suc文件, 因此该代码无法在在线IDE上运行。

你可以在系统上运行此代码以检查其工作情况。

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

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

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: