Guest User

Untitled

a guest
Feb 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. package priv.mm.io;
  2.  
  3. import java.io.IOException;
  4. import java.nio.MappedByteBuffer;
  5. import java.nio.channels.FileChannel;
  6. import java.nio.charset.StandardCharsets;
  7. import java.nio.file.Path;
  8. import java.nio.file.Paths;
  9.  
  10. public class MemoryMappedFileDemo {
  11.  
  12. public static void main(String[] args) {
  13. Path path = Paths.get("/Users/maomao/IdeaProjects/Demo/src/main/java/priv/mm/io/声声慢.txt");
  14. try (FileChannel channel = FileChannel.open(path)) {
  15. final long size = channel.size();
  16. final byte[] bytes = new byte[(int) size];
  17. MappedByteBuffer byteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
  18. for (int i = 0; i < size; i++) {
  19. bytes[i] = byteBuffer.get();
  20. }
  21. System.out.println(new String(bytes, StandardCharsets.UTF_8));
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. }
Add Comment
Please, Sign In to add comment