import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class NewIOTesting {
public static void main(String[] args) throws Exception, Throwable{
long bytelength = 1024*4;
String s = "abcdefghijklmnopqrstuvwxyz\n";
FileChannel fc = new RandomAccessFile("./data/2","rw").getChannel();
MappedByteBuffer byteBuff = fc.map(FileChannel.MapMode.READ_WRITE,0,bytelength );
for(int i =0; i<3000;i++){
if((s.length()+i)>=byteBuff.remaining()){
System.out.println("current i: "+i+" current position: "+byteBuff.position());
System.out.println("buff length is not enough! Will move to next");
System.out.println("buff size "+ byteBuff.limit()+" buff cap "+byteBuff.capacity());
int empty = byteBuff.capacity()- byteBuff.position();
byteBuff.force();
byteBuff.clear();
byteBuff = fc.map(FileChannel.MapMode.READ_WRITE,fc.size()-empty,bytelength );
System.out.println("After add new buff position: "+byteBuff.position());
}
byteBuff.put((i+s).getBytes());
}
fc.close();
}
}