我从oracle docs学习java。
我现在正在学习BufferedInputStream
available();
方法
我拿了示例代码并编写了以下代码
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。
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:1)
我在我的日食中使用了你的代码,输出似乎很好:
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