String lineseperator = java.security.AccessController .doPrivileged(new sun.security.action.GetPropertyAction( "line.separator"));

Access restriction: The constructor 'GetPropertyAction(String)' is not API

Access restriction on class due to restriction on required library rt.jar?

  1. Go to the Build Path settings in the project properties.
  2. Remove the JRE System Library
  3. Add it back; Select "Add Library" and select the JRE System Library. The default worked for me.
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode; public class memorymapfiledemo {
final static String filepath = "/home/hadoop/test/java.txt"; public static void main(String[] args) throws IOException {
writefile(filepath);
readfile(filepath);
} static void writefile(String _filepath) throws IOException {
File _file = new File(_filepath);
RandomAccessFile raf = new RandomAccessFile(_file, "rw");
FileChannel fc = raf.getChannel();
int buffersize = 1024 * 8;
CharBuffer cb = fc.map(MapMode.READ_WRITE, 0, buffersize)
.asCharBuffer();
String lineseperator = java.security.AccessController
.doPrivileged(new sun.security.action.GetPropertyAction(
"line.separator"));
String content = "";
long offset = 0;
for (int i = 1; i <= 1000; i++) {
content = "Line" + i + " hello java" + lineseperator;
if ((cb.limit() - cb.position()) < content.length()) {
offset += cb.position();
cb = fc.map(MapMode.READ_WRITE, offset, buffersize)
.asCharBuffer();
}
cb.put(content);
}
fc.close();
raf.close();
} static void readfile(String _filepath) throws IOException {
File _file = new File(_filepath);
RandomAccessFile raf = new RandomAccessFile(_file, "rw");
FileChannel fc = raf.getChannel();
long totalsize = fc.size();
int buffersize = 1024 * 8;
long offset = 0;
CharBuffer cb = fc.map(MapMode.READ_ONLY, 0, buffersize).asCharBuffer();
while (offset < totalsize) {
while (cb.hasRemaining()) {
System.out.print(cb.get());
}
offset += cb.position();
cb = fc.map(MapMode.READ_ONLY, offset, buffersize).asCharBuffer();
}
fc.close();
raf.close();
}
}

java Memorymapfile demo的更多相关文章

  1. 微信公众号Java接入demo

    微信公众号Java接入demo 前不久买了一台服务,本来是用来当梯子用的,后来买了一个域名搭了一个博客网站,后来不怎么在上面写博客一直闲着,最近申请了一个微信公众号就想着弄点什么玩玩.周末没事就鼓捣了 ...

  2. Kafka2.4发布——新特性介绍(附Java Api Demo代码)

    新功能 允许消费者从最近的副本进行获取 为 Consumer Rebalance Protocol 增加对增量协同重新均衡(incremental cooperative rebalancing)的支 ...

  3. appium+java+junit demo运行

    对java熟悉一些,所以想用java把appium给做起来.今天用myeclipse给环境和Demo弄了一下,网上没有一篇全套资料的. 各块环境的搭建: 1.安装appium客户端,省略下载和安装步骤 ...

  4. 微博开发平台java SDK demo学习之friendships

    本文解释了在java SDK的demo中与feiendships有关的功能 截图如下: 关注一个用户(需要知道该用户uid) 取消关注一个用户(用户uid) 获取用户粉丝列表(授权用户的screen_ ...

  5. 快递鸟物流单号自动识别接口JAVA对接demo

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  6. java迭代器demo

    package cn.aust.zyw.demo; import java.util.Iterator; /** * Created by zyw on 2016/2/16. * Iterator模式 ...

  7. Java Design Demo -简单的队列-异步多任务队列(java android)

    简单的单线程队列 -- 工作的时候遇到劣质打印机.给打印机发消息,打印机就会打印,如果在打印机还在打印的时候,就 再发消息打印,就会出现消息丢失.所以需要给上一个任务一些处理的间隔时间. 单线程的消息 ...

  8. Floyd算法java实现demo

    Floyd算法java实现,如下: https://www.cnblogs.com/Halburt/p/10756572.html package a; /** * ┏┓ ┏┓+ + * ┏┛┻━━━ ...

  9. solr环境搭建及java小demo

    一配置solr环境 1.下载solr 2.配置solr(最好单独分离出一个tomcat,一台机器启动多个tomcat参见:http://www.cnblogs.com/lxlwellaccessful ...

随机推荐

  1. csharp: Export DataSet into Excel and import all the Excel sheets to DataSet

    /// <summary> /// Export DataSet into Excel /// </summary> /// <param name="send ...

  2. 基于FreeBSD 64位内核的kFreeBSD无法在Virtualbox下安装

    ArchBSD同上 感谢大A(豆瓣)的投稿 :)

  3. Little Jumper---(三分)

    Description Little frog Georgie likes to jump. Recently he have discovered the new playground that s ...

  4. Picasso

    1.简介 Picasso是Square公司出品的一个强大的图片下载和缓存图片库1)在adapter中需要取消已经不在视野范围的ImageView图片资源的加载,否则会导致图片错位,Picasso已经解 ...

  5. 干净的停止tomcat/java应用程序

    通常在使用了jdbc或者netty的应用程序中,当shutdown tomcat或java应用程序时,会出现无法停止的情况,报类似如下错误: 严重: The web application [] re ...

  6. MySQL Plugin 'InnoDB' init function returned error一例

    早上上班后,测试说演示环境挂了,维护上去看了下,启动报错了: XXXXXX08:30:47 mysqld_safe Starting mysqld daemon with databases from ...

  7. QTimer太让人失望了,一秒触发一次事件都不准确。。

    今天做项目中,我用QTimer来模拟数据生成,在另外的设备上接受.另外设备上有时1秒读不到数据,查询原因很久,终于发现是QTimer的问题. 测试代码如下 有兴趣同学可以自己试试. t = new Q ...

  8. spring mvc 框架搭建及详解

    现 在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不 ...

  9. hdu 2952 Counting Sheep

    本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=2952 题意:上下左右4个方向为一群.搜索有几群羊 #include <stdio.h> # ...

  10. 一道灵活的css笔试题

    今天在网上看到一css笔试题,乍一看很简单,实则内部暗藏玄机,题目大概是:九宫格,每格长宽50px,边框宽度5px,鼠标经过边框变红,效果如下: 鼠标路过时: 以下是代码(如有不足之处望多加指正) & ...