Java MappedByteBuffer写入文件不工作

Java MappedByteBuffer写入文件不工作,java,java-8,java-7,nio,nio2,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"

我很难理解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();
    }
}}
公共类MappedByteBufferExample{
公共静态void main(字符串[]args){
try(RandomAccessFile=newrandomAccessFile(“resources\\MappedByteBufferExample.txt”,“rw”);
FileChannel FileChannel=file.getChannel();){
long size=fileChannel.size();
MappedByteBuffer mappedBuffer=fileChannel.map(MapMode.READ\u WRITE,0,size);
System.out.println(“文件中的文本:-”);
while(mappedBuffer.remaining()>0){
System.out.print((char)mappedBuffer.get());
}
System.out.println(“\n”);
对于(int index=0;index<(mappedBuffer.limit()/2);index++){
字节b1=mappedBuffer.get(索引);
字节b2=mappedBuffer.get(mappedBuffer.limit()-index-1);
mappedBuffer.put(索引,b2);
mappedBuffer.put(mappedBuffer.limit()-index-1,b1);
}
//mappedBuffer.force();我尝试了这个,但结果没有改变。
//load();我尝试了这个方法,但结果没有改变。
load();
flip();
System.out.println(“文本反转:--”;
while(mappedBuffer.remaining()>0){
System.out.print((char)mappedBuffer.get());
}
}捕获(IOE异常){
e、 printStackTrace();
}
}}
该文件的内容是-玫瑰是红色的
执行此文件时,控制台的输出为:

文件中的文本:-----

玫瑰是红色的

反向文本:-----

!!塞索尔时代酒店

但文件的内容不变。 第二次执行此程序时,控制台的输出为:

文件中的文本:-----

!!塞索尔时代酒店

反向文本:-----

玫瑰是红色的

该文件的内容仍保持不变。 我一次尝试一个load()和force()方法,但结果没有变化 以下是我的问题:

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

2) 此程序需要进行哪些更改才能将数据写入文件


3) MappedByteBuffer在第二次迭代时读取反转文本的原因/方式,尽管文件内容没有反转?

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

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

由于您的实际问题已经解决,请注意:

  • 自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();
}
try(FileChannel FileChannel=FileChannel.open(
get(“resources\\MappedByteBufferExample.txt”),
StandardOpenOption.READ,StandardOpenOption.WRITE){
long size=fileChannel.size();
MappedByteBuffer mappedBuffer=fileChannel.map(MapMode.READ\u WRITE,0,size);
System.out.println(“文件中的文本:-”);
System.out.append(Charset.defaultCharset().decode(mappedBuffer)).println(“\n”);

对于(int index1=0,index2=(int)size-1;index1由于您的实际问题已经解决,请注意:

  • 自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();
}
try(FileChannel FileChannel=FileChannel.open(
get(“resources\\MappedByteBufferExample.txt”),
StandardOpenOption.READ,StandardOpenOption.WRITE){
long size=fileChannel.size();
MappedByteBuffer mappedBuffer=fileChannel.map(MapMode.READ\u WRITE,0,size);
System.out.println(“文件中的文本:-”);
System.out.append(Charset.defaultCharset().decode(mappedBuffer)).println(“\n”);

对于(int index1=0,index2=(int)size-1;不需要index1load和flip。println只需要使用
flip
,因为它会重置读取位置。但是,只有在关闭时文本才会写回(在这种情况下)。“\r\n”将变为“\n\r”。您没有提供文件未更改的证据,也没有提供文件已更改的主要证据。不需要加载和翻转。
flip
仅用于println,因为它会重置读取位置。但是,只有在关闭时,文本才会写回(在本例中)。“\r\n”将变为“\n\r”.您没有提供文件未更改的证据,并且