博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java Memorymapfile demo
阅读量:5030 次
发布时间:2019-06-12

本文共 2517 字,大约阅读时间需要 8 分钟。

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

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

  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();    }}

转载于:https://www.cnblogs.com/huaxiaoyao/p/4305007.html

你可能感兴趣的文章
js基础
查看>>
基础_模型迁移_CBIR_augmentation
查看>>
第二次寒假作业
查看>>
类与 对象 概念 break continue
查看>>
tensorRT使用python进行网络定义
查看>>
[转]从程序员到项目经理(三):认识项目经理
查看>>
深度分析如何在Hadoop中控制Map的数量
查看>>
dede判断当前文章
查看>>
mpvue学习笔记
查看>>
[LeetCode] 628. Maximum Product of Three Numbers_Easy
查看>>
[Java in NetBeans] Lesson 06. Custom classes
查看>>
[AngularFire2 & Firestore] Example for collection and doc
查看>>
[Javascript] The "this" keyword
查看>>
ElasticSearch-5.3.1集群环境搭建,安装ElasticSearch-head插件,安装错误解决
查看>>
sharepoint Report使用共享数据源部署报错
查看>>
C++ Primer 5th 第16章 模板与泛型编程
查看>>
22个Web 在线编辑器[转]
查看>>
解决“The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path”问题...
查看>>
GitHub本地上传代码教程
查看>>
bzoj1045 [HAOI2008] 糖果传递
查看>>