null
JAVA伊奥。字节数组输入流 类包含所有缓冲区,其中包含要从输入流读取的字节。ByteArrayInputStream类方法没有IO异常。即使在关闭流之后也可以调用此类的方法,这对类方法没有影响。
宣言:
public class ByteArrayInputStream extends InputStream
领域
- 受保护字节[]buf: 流的创建者提供的字节数组。
- 受保护整数计数: 索引大于输入流缓冲区中最后一个有效字符。
- 受保护整数标记: 流中当前标记的位置。
- 受保护的int pos: 这是要从输入流缓冲区读取的下一个字符的索引。
施工人员:
- ByteArrayInputStream(字节[]缓冲区): 创建ByteArrayInputStream以使用缓冲区数组–“buffer”。
- ByteArrayInputStream(字节[]buf,整数偏移量,整数长度): 创建ByteArrayInputStream,它使用“缓冲区”的某些部分,即缓冲区数组
方法:
- 马克(int-arg):Java。伊奥。ByteArrayInputStream。标记(int arg) 标记输入流的当前位置。它设置读取限制,即在标记位置无效之前可以读取的最大字节数。 语法:
public void mark(int arg)Parameters :arg : integer specifying the read limit of the input StreamReturn : void
- read():java。伊奥。ByteArrayInputStream。读() 从输入流中读取下一个字节的数据。返回的字节值范围为0到255。如果由于到达流的结尾而没有字节可用,则返回值-1。方法不会阻塞 语法:
public 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。伊奥。ByteArrayInputStream。关闭() 关闭输入流并将与此流关联的系统资源释放到垃圾收集器。 语法:
public void close()Parameters :------Return : voidException :-> IOException : If I/O error occurs.
- 读取(字节[]缓冲区,int偏移量,int maxlen):Java。伊奥。ByteArrayInputStream。读取(字节[]缓冲区,整数偏移量,整数最大值) 从位置“offset”到maxlen读取输入流的“buffer”字节数组。 语法:
public int read(byte[] buffer, int offset, int maxlen)Parameters :arg : array whose number of bytes to be readoffset : starting position in buffer from where to readmaxlen : maximum no. of bytes to be readReturn : 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。伊奥。ByteArrayInputStream。重置() 由mark()方法调用。它将输入流重新定位到标记的位置。 语法:
public void reset()Parameters :----Return : voidException :-> IOException : If I/O error occurs.
- markSupported():Java。伊奥。ByteArrayInputStream。markSupported() 方法测试此输入流是否支持标记和重置方法。ByteArrayInputStreamInputStream的markSupported方法始终返回true 语法:
public boolean markSupported()Parameters :-------Return : true if input stream supports the mark() and reset() method else,false
- 跳过(长arg):Java。伊奥。ByteArrayInputStream。跳过(长arg) 跳过 阿格 输入流中的字节。 语法:
public long skip(long arg)Parameters :arg : no. of bytes to be skippedReturn : skip bytes.Exception :-> IOException : If I/O error occurs.
- available():Java。伊奥。ByteArrayInputStream。可用() 告诉要从输入流读取的字节总数 语法:
public int available()Parameters :-----------Return : total no. of bytes to be readException :-----------
Java程序解释ByteArrayInputStream类方法:
JAVA
// Java program illustrating the working of ByteArrayInputStream method // mark(), read(), skip(), available() // markSupported(), close(), reset() import java.io.*; public class NewClass { public static void main(String[] args) throws Exception { byte [] buffer = { 71 , 69 , 69 , 75 , 83 }; ByteArrayInputStream geek = null ; try { geek = new ByteArrayInputStream(buffer); // Use of available() method : telling the no. of bytes to be read int number = geek.available(); System.out.println( "Use of available() method : " + number); // Use of 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()); // Use of mark() : geek.mark( 0 ); // Use of skip() : it results in skipping 'k' from "GEEKS" 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()); // Use of markSupported boolean check = geek.markSupported(); System.out.println( "markSupported() : " + check); if (geek.markSupported()) { // Use of reset() method : repositioning the stream 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 except) { // in case of I/O error except.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(); } } } } |
输出:
Use of available() method : 5Char : GChar : EChar : Eskip() method comes to playmark() method comes to playChar : SmarkSupported() : truereset() invokedChar : KChar : Sgeek.markSupported() supported reset() : true
下一篇文章: 伊奥。Java中的ByteArrayOutputStream()类
本文由 莫希特·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END