描述
這個java.io.BufferedInputStream.available()方法返回要從輸入流中讀取的剩餘字節數,而不會因下一次調用此輸入流的方法而阻塞。
聲明
以下是聲明java.io.BufferedInputStream.available()方法。
public int available()
返回值
該方法返回number of bytes仍然可以在不阻塞的情況下從此輸入流中讀取。
異常
IOException− 如果發生 I/O 錯誤。
示例
下麵的例子展示了 java.io.BufferedInputStream.available() 方法的用法。
package com.tutorialspoint;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class BufferedInputStreamDemo {
public static void main(String[] args) throws Exception {
BufferedInputStream bis = null;
try {
// open input stream test.txt for reading purpose.
inStream = new FileInputStream("c:/test.txt");
// input stream is converted to buffered input stream
bis = new BufferedInputStream(inStream);
// read until a single byte is available
while( bis.available() > 0 ) {
// get the number of bytes available
Integer nBytes = bis.available();
System.out.println("Available bytes = " + nBytes );
// read next available character
char ch = (char)bis.read();
// print the read character.
System.out.println("The character read = " + ch );
}
} catch(Exception e) {
e.printStackTrace();
} finally {
// releases any system resources associated with the stream
if(inStream!=null)
inStream.close();
if(bis!=null)
bis.close();
}
}
}
假設我們有一個文本文件c:/test.txt,其內容如下。該文件將用作我們示例程序的輸入 -
ABCDE
讓我們編譯並運行上麵的程序,這將產生以下結果 -
Available bytes = 5 The character read = A Available bytes = 4 The character read = B Available bytes = 3 The character read = C Available bytes = 2 The character read = D Available bytes = 1 The character read = E
相關用法
- Java Java.io.BufferedInputStream.close()用法及代碼示例
- Java Java.io.BufferedInputStream.reset()用法及代碼示例
- Java Java.io.BufferedInputStream.read()用法及代碼示例
- Java Java.io.BufferedInputStream.skip()用法及代碼示例
- Java Java.io.BufferedReader.Close()用法及代碼示例
- Java Java.io.BufferedWriter.newLine()用法及代碼示例
- Java Java.io.BufferedReader.read()用法及代碼示例
- Java Java.io.BufferedOutputStream.flush()用法及代碼示例
- Java Java.io.BufferedReader.skip()用法及代碼示例
- Java Java.io.BufferedWriter.close()用法及代碼示例
- Java Java.io.BufferedReader.markSupported()用法及代碼示例
- Java Java.io.BufferedReader.ready()用法及代碼示例
- Java Java.io.BufferedOutputStream.Write()用法及代碼示例
- Java Java.io.BufferedWriter.flush()用法及代碼示例
- Java Java.io.BufferedReader.readline()用法及代碼示例
- Java Java.io.BufferedReader.mark()用法及代碼示例
- Java Java.io.BufferedWriter.write()用法及代碼示例
- Java Java.io.BufferedReader.reset()用法及代碼示例
- Java Java.io.ByteArrayInputStream.reset()用法及代碼示例
- Java Java.io.ByteArrayInputStream.mark()用法及代碼示例
注:本文由純淨天空篩選整理自 Java.io.BufferedInputStream.available() Method Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。