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?
- Go to the Build Path settings in the project properties.
- Remove the JRE System Library
- Add it back; Select "Add Library" and select the JRE System library. The default worked for me.
ImportJava.io.File;Importjava.io.IOException;ImportJava.io.RandomAccessFile;ImportJava.nio.CharBuffer;ImportJava.nio.channels.FileChannel;ImportJava.nio.channels.FileChannel.MapMode; Public classMemorymapfiledemo {Final StaticString filepath = "/home/hadoop/test/java.txt"; Public Static voidMain (string[] args)throwsIOException {WriteFile (filepath); ReadFile (filepath); } Static voidWriteFile (String _filepath)throwsIOException {File _file=NewFile (_filepath); Randomaccessfile RAF=NewRandomaccessfile (_file, "RW"); FileChannel FC=Raf.getchannel (); intbuffersize = 1024 * 8; Charbuffer CB= Fc.map (mapmode.read_write, 0, buffersize). Ascharbuffer (); String Lineseperator=Java.security.AccessController. doprivileged (NewSun.security.action.GetPropertyAction ("Line.separator")); String content= ""; LongOffset = 0; for(inti = 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 voidReadFile (String _filepath)throwsIOException {File _file=NewFile (_filepath); Randomaccessfile RAF=NewRandomaccessfile (_file, "RW"); FileChannel FC=Raf.getchannel (); LongTotalSize =fc.size (); intbuffersize = 1024 * 8; LongOffset = 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