Java.io.FilterWriter.available() 方法



描述

java.io.FilterWriter.available() 方法返回可從該輸入流讀取的位元組數,而不會被該輸入流的下一個方法呼叫阻塞。

宣告

以下是 java.io.FilterWriter.available() 方法的宣告:

public int available()

引數

返回值

該方法返回可讀取的位元組數。

異常

IOException − 如果發生 I/O 錯誤。

示例

以下示例演示了 java.io.FilterWriter.available() 方法的使用。

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) throws Exception {
      InputStream is = null;
      int i = 0;
      char c;
      
      try {
         // new input stream created
         is = new FileInputStream("C://test.txt");
         
         // read till the end of the stream
         while((i = is.read())!=-1) {
         
            // convert integer to character
            c = (char)i;
            
            // print
            System.out.println("Character Read: "+c);
         }
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases system resources associated with this stream
         if(is!=null)
            is.close();
      }
   }
}

假設我們有一個文字檔案 c:/test.txt,其內容如下。此檔案將用作我們示例程式的輸入:

ABCDEF

讓我們編譯並執行上述程式,這將產生以下結果:

Character Read: A
Character Read: B
Character Read: C
Character Read: D
Character Read: E
Character Read: F
java_io_inputstream.htm
廣告