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. css中visiblity和display异同

    visiblity 是设置元素的可见性,即可见 / 隐藏:隐藏后元素所占有位置保留: display 是设置元素按什么样的方式来显示,是按块显示,显示成一条线的形式,显示为“消失”等等,当displa ...

  2. 获取datatable更新之前的数据

    string dd = ds.Tables[0].Rows[0][0, DataRowVersion.Original].ToString() ;

  3. JPA学习(3)JPA API

    在我们的jpa的helloworld中,我们看到了简单的一个jpa保存操作,下面就来好好学习一下,JPA最主要的几个类 1.基本的几个类: ①:Persistence 类是用于获取 EntityMan ...

  4. php中的抛出异常和捕捉特定类型的异常

    测试环境:PHP5.5.36  Safari 9.1.2   异常捕获,在现在很多ide工具里都可以用快捷键很方便的添加上,防止用户看到自己看不懂的报错甚至莫名其妙崩溃,导致用户体验不好. 哪怕显示一 ...

  5. java.util.TimeZone 新加方法 getTimeZone(ZoneId zoneId) 导致的问题

    最近接受的项目中用spring配置了一个TimeZone对象: <bean id ="timeZone4Job" class="java.util.TimeZone ...

  6. Play常用代码片段 http://www.anool.net/?p=625

    持续更新中: (1)按照降序查询: List<Entity> entities= Entity.find("order by id desc").fetch(2);   ...

  7. CMD和AMD

    CMD是国内玉伯在开发SeaJS的时候提出来的,属于CommonJS的一种规范,此外还有AMD,其对于的框架是RequireJS. 二者的异同之处: 二者都是异步(Asynchronuous Modu ...

  8. CRM2013版本 IOS APP 说明(IPhone、IPad)

    CRM2013版本 IOS APP 说明(IPhone.IPad) IPhone版本 首页 CRM APP在登录时输入账号信息,可以进行首面.其首页显示内容可以在CRM后台设置. 系统默认显示:Pho ...

  9. SharePoint固定的Footer

    原文地址:http://www.eliostruyf.com/sticky-footer-solution-for-sharepoint-2013/ 照搬全文: OFFICE 365 & SH ...

  10. C语言中void*详解及应用

    void在英文中作为名词的解释为“空虚:空间:空隙”:而在C语言中,void被翻译为“无类型”,相应的void *为“无类型指针”.void似乎只有“注释”和限制程序的作用,当然,这里的“注释”不是为 ...