我很難理解用MappedByteBuffer讀寫。 這裏是我讀取本地文件的內容,並假設其內容顛倒的類。我正在使用java版本8.MappedByteBuffer寫入文件不起作用
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時代sesoR
但文件的內容是不變的。 論第二次執行該程序,輸出到控制檯:
從文件文本:-----
DER時代sesoR
文字反轉!-----
玫瑰是紅的!
該文件的內容仍然沒有改變。 我試過load()和力()方法之一的時間和雙方,但在結果 這裏沒有改變是我的問題:
1)爲什麼本地文件的內容沒有改變?
2)該程序需要將數據寫入文件中需要做什麼修改?
3)爲什麼/如何在第二次迭代中MappedByteBuffer讀取反轉的文本,但文件內容沒有反轉?
不需要加載和翻轉。 println只需要「flip」,因爲它會重置讀取位置。但是隻有在最後纔會寫回文本(在這種情況下)。 「\ r \ n」將變成「\ n \ r」。 –
你沒有提供證據表明該文件沒有被更改,並且是一個主要的證據。 – EJP