MappedByteBuffer写入文件无效

时间:2016-10-02 12:30:57

标签: java java-8 java-7 nio nio2

我很难用MappedByteBuffer理解读写。 这是我所拥有的类,它读取本地文件的内容并假设要反转其内容。我使用的是java版本8。

public class MappedByteBufferExample {
public static void main(String[] args) {
    try (RandomAccessFile file = new RandomAccessFile("resources\\MappedByteBufferExample.txt", "rw");
            FileChannel fileChannel = file.getChannel();) {
        long size = fileChannel.size();
        MappedByteBuffer mappedBuffer = fileChannel.map(MapMode.READ_WRITE, 0, size);

        System.out.println("Text from File : -----");
        while (mappedBuffer.remaining() > 0) {
            System.out.print((char) mappedBuffer.get());
        }
        System.out.println("\n");

        for (int index = 0; index < (mappedBuffer.limit() / 2); index++) {
            byte b1 = mappedBuffer.get(index);
            byte b2 = mappedBuffer.get(mappedBuffer.limit() - index - 1);
            mappedBuffer.put(index, b2);
            mappedBuffer.put(mappedBuffer.limit() - index - 1, b1);
        }
        //mappedBuffer.force();  I tried with this but no change in result.
        //mappedBuffer.load(); I tried with this but no change in result.
        mappedBuffer.load();
        mappedBuffer.flip();
        System.out.println("Text Reversed : -----");
        while (mappedBuffer.remaining() > 0) {
            System.out.print((char) mappedBuffer.get());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}}

文件的内容是 - 玫瑰是红色的!
执行此文件输出到控制台是:

文件中的文字:-----

玫瑰红了!

文字反转:-----

!deR era sesoR

但是文件的内容没有变化。 在第二次执行该程序时,控制台的输出为:

文件中的文字:-----

!deR era sesoR

文字反转:-----

玫瑰红了!

文件内容仍未更改。 我一次尝试了一个load()和force()方法,但结果都没有变化 以下是我的问题:

1)为什么本地文件的内容没有改变?

2)该程序需要对数据写入文件进行哪些更改?

3)为什么/如何在第二次迭代中读取反向文本的MappedByteBuffer,尽管文件内容没有反转?

2 个答案:

答案 0 :(得分:3)

文件的内容已更改。通常的编辑器(如notepad ++或eclipse)不会注释更改,因为使用RandomAccessFile不会更改文件日期时间。但如果你真的在运行后手动重新加载文件,你应该看到更改。

答案 1 :(得分:0)

由于你的实际问题已经解决,一些评论:

  • 从Java 1.7开始,您不需要RandomAccessFile绕道来打开FileChannel
  • Charset API允许您将ByteBuffer的内容正确转换为字符 (将byte转换为char仅适用于例如当前Windows代码页的子集,而不需要手动迭代字节
  • 交换循环可以通过使用两个索引直接编写,而不是在每次迭代中重新计算第二个索引(和循环的结束索引)

将这些要点放在一起,简化的变体如下:

try(FileChannel fileChannel = FileChannel.open(
        Paths.get("resources\\MappedByteBufferExample.txt"),
        StandardOpenOption.READ, StandardOpenOption.WRITE)) {
    long size = fileChannel.size();
    MappedByteBuffer mappedBuffer = fileChannel.map(MapMode.READ_WRITE, 0, size);

    System.out.println("Text from File : -----");
    System.out.append(Charset.defaultCharset().decode(mappedBuffer)).println("\n");

    for(int index1 = 0, index2=(int)size-1; index1<index2; index1++, index2--) {
        byte b1 = mappedBuffer.get(index1), b2 = mappedBuffer.get(index2);
        mappedBuffer.put(index1, b2).put(index2, b1);
    }
    mappedBuffer.force();
    mappedBuffer.flip();
    System.out.println("Text Reversed : -----");
    System.out.append(Charset.defaultCharset().decode(mappedBuffer)).println("\n");
} catch (IOException e) {
    e.printStackTrace();
}

请记住,无论是否已将文件写入文件,读取循环都将始终显示已更改的状态。因此,force()是放在读取循环之前还是放在块的末尾并不重要。