This commit is contained in:
wyl
2023-04-23 17:28:54 +08:00
parent 760582618f
commit 73d5c27005
19 changed files with 1033 additions and 11 deletions
@@ -7,15 +7,16 @@ import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class Sequential {
public static int fileWrite(String filePath, String content, int index) {
public static int fileWrite(String filePath, String content) {
File file = new File(filePath);
RandomAccessFile randomAccessTargetFile;
MappedByteBuffer map;
try {
randomAccessTargetFile = new RandomAccessFile(file, "rw");
FileChannel targetFileChannel = randomAccessTargetFile.getChannel();
map = targetFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, (long) 1024 * 1024 * 1024);
map.position(index);
byte[] bytes = content.getBytes();
map = targetFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, bytes.length);
map.position(0);
map.put(content.getBytes());
return map.position();
}
@@ -16,18 +16,17 @@ public class FileTest {
public void SequentialWriteFile() {
Integer index = 0;
File file = new File("./wyl.log");
MappedByteBuffer map;
try (RandomAccessFile randomAccessTargetFile = new RandomAccessFile(file, "rw")) {
FileChannel targetFileChannel = randomAccessTargetFile.getChannel();
map = targetFileChannel.map(FileChannel.MapMode.READ_WRITE, pos.get(), (long) 1024 * 1024 * 1024);
if (map.isLoaded()) {
map = targetFileChannel.map(FileChannel.MapMode.READ_WRITE, pos.get(), 2);
while (index<2)
{
map.position(index);
map.put("1".getBytes());
index = map.position();
}
map.position(index);
map.put("123\n".getBytes());
final int position = map.position();
}
catch (Exception e) {
e.printStackTrace();