gpt4 book ai didi

java - BufferedInputStream 的 available() 行为不符合预期

转载 作者:行者123 更新时间:2023-12-01 11:26:16 24 4
gpt4 key购买 nike

我正在从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。

谁能帮我解决这个问题吗?

最佳答案

我在我的 Eclipse 中使用了你的代码,输出似乎很好:

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

关于java - BufferedInputStream 的 available() 行为不符合预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30778897/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com