Popular Posts
DataList paging //利用PageDataSource來做分頁功能 PagedDataSource pds = new PagedDataSource(); //將PageDataSource綁定SqlDataSource pds.DataSource = SqlDataSource1.Selec... DNS SERVER LIST Google 8.8.8.8 8.8.4.4 TWNIC 192.83.166.11 211.72.210.250 HiNet 168.95.1.1 168.95.192.1 Seednet 北區 DNS (台北, 桃園, 新竹, 宜蘭, 花蓮, 苗栗) 139.... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i...
Stats
Shared memory (file base) in Java
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SmWrite {

    public static void main(String[] args) throws Exception {
        File file = new File("c:/mem.txt");
        int length = (int) file.length();
        String mode = "rw";
        FileChannel fc = new RandomAccessFile(file, mode).getChannel();
        MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, length);
        String format = "yyyy/MM/dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        while (true) {
            String now = sdf.format(new Date());
            byte[] b = now.getBytes();
            mbb.rewind();
            mbb.put(b, 0, b.length);
            mbb.force();
            Thread.sleep(1000);
        }
    }
}
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.text.SimpleDateFormat;

public class SmRead {

    public static void main(String[] args) throws Exception {
        File file = new File("c:/mem.txt");
        int length = (int) file.length();
        String mode = "rw";
        FileChannel fc = new RandomAccessFile(file, mode).getChannel();
        MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, length);
        String format = "yyyy/MM/dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        byte[] b = new byte[format.length()];
        while (true) {
            mbb.rewind();
            mbb.get(b, 0, b.length);
            String now = new String(b);
            System.out.println(now);
            Thread.sleep(1000);
        }
    }
}
reference : http://www.javaworld.com.tw/jute/post/view?bid=29&id=273887