BufferedInputStream的available

BufferedInputStream的available

我正在从oracle文档学习Java。

我正在学习BufferedInputStreamavailable();方法

我获取了示例代码并制定了以下代码

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

public class BufferInput {
   public static void main(String[] args) throws Exception {

      InputStream inStream = null;
      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();
      }
   }
}


当我运行这段代码时

它显示以下输出:

Available bytes = 2
The character read = V
Available bytes = 1
The character read = A


但是在我的test.txt文件中的内容是SELVA。

谁能帮我解决这个问题?

最佳答案

我在日食中使用了您的代码,输出似乎正常:

Available bytes = 5
The character read = s
Available bytes = 4
The character read = e
Available bytes = 3
The character read = l
Available bytes = 2
The character read = v
Available bytes = 1
The character read = a

10-08 19:48