test
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user