我有一个问题从java输入流中读取。我有大小为1024的缓冲器,和大小29K-31K的输入流。我在一个循环中读取InputStream的,但我只获得了29个字节后的第一次读取,39为二读,并没有什么。同样的行为重复不同的InputStreams。 (我将数据写入一个输出流,但我不看这是如何影响第一读取)

        int bytesRead = 0;
        byte[] byteBuf = new byte[1024];

        OutputStream fileStream = FileUtil.openFileForWrite(saveTo);

        bytesRead = reader.read(byteBuf);
        while(bytesRead!=-1){
            fileStream.write(byteBuf, 0, bytesRead);
            bytesRead = reader.read(byteBuf);
        }

我是什么失踪?

任何帮助理解:)

有帮助吗?

解决方案

你从哪里得到的输入流?你怎么知道它的29K-31K?

您的代码看起来合理的我,虽然我一般结构中的循环略有不同,以避免read呼叫的重复。

其他提示

您是否尝试使用的ReadLine()而不是读()?

Path file = ...;
InputStream in = null;
try {
    in = file.newInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String line = null;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
} catch (IOException x) {
    System.err.println(x);
} finally {
    if (in != null) in.close();
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top