为什么缓冲区没有写入FileChannel

问题描述 投票:2回答:1

我现在正在学习java NIO,我找到了一个例子来解释FileChannel的收集操作,如下所示:

public class ScattingAndGather {
    public static void main(String args[]) {
        gather();
    }

    public static void gather() {
        ByteBuffer header = ByteBuffer.allocate(10);
        ByteBuffer body = ByteBuffer.allocate(10);

        byte[] b1 = { '0', '1' };
        byte[] b2 = { '2', '3' };
        header.put(b1);
        body.put(b2);

        ByteBuffer[] buffs = { header, body };

        FileOutputStream os = null;
        FileChannel channel = null;
        try {
            os = new FileOutputStream("d:/scattingAndGather.txt");
            channel = os.getChannel();
            channel.write(buffs);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (channel != null) {
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

虽然结果显示,文件已经创建,但是它是空的,应该是0123,这个例子有什么不对?

java nio filechannel
1个回答
2
投票

问题出现在你永远不会重置缓冲区的位置。

当你创建你的ByteBuffer对象时,它们从零位开始。无论何时向其中添加内容,它们的位置都会提前,这意味着当您尝试将它们写出来时,它们会报告不再需要读取的字节数。因此,在进行任何类型的此类操作之前,您需要重置其位置。

Buffer提供了一些最容易使用的方法是flip()。正如您在here文档中看到的,它的用法如下:

翻转此缓冲区。限制设置为当前位置,然后位置设置为零。如果定义了标记,则将其丢弃。

在一系列通道读取或放置操作之后,调用此方法以准备一系列通道写入或相对获取操作

因此在写出来之前你需要翻转它们。此外,由于您正在尝试java.nio,我不明白为什么您不应该使用try with resources语句来管理您的各种资源。这样,您将避免过多关闭可以手动自动关闭的资源的锅炉板代码。

使用那些代码可以显着缩小并且更具可读性:

public static void gather() {
    ByteBuffer header = ByteBuffer.allocate(10);
    ByteBuffer body = ByteBuffer.allocate(10);

    byte[] b1 = { '0', '1' };
    byte[] b2 = { '2', '3' };
    header.put(b1);
    body.put(b2);

    //flip buffers before writing them out.
    header.flip();
    body.flip();
    ByteBuffer[] buffs = { header, body };

    try(FileOutputStream os = new  FileOutputStream("d:/scattingAndGather.txt");
 FileChannel channel = os.getChannel()) {
    channel.write(buffs);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.