Here you can find the source of write(Path file, CharBuffer data, Charset cs)
public static void write(Path file, CharBuffer data, Charset cs) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.CharBuffer; import java.nio.IntBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.nio.file.Path; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class Main { private static Map<String, FileChannel> FILES = new ConcurrentHashMap<String, FileChannel>(); public static void write(Path file, byte[] data) throws IOException { FileChannel fc = getFileChannel(file, "rw"); MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, data.length); mbb.put(data);/*from w w w. j a v a2 s .c o m*/ fc.close(); } public static void write(Path file, CharBuffer data, Charset cs) throws IOException { FileChannel fc = getFileChannel(file, "rw"); MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, data.length()); mbb.put(cs.encode(data)); fc.close(); } public static void write(Path file, List<String> data, Charset cs) throws IOException { FileChannel fc = getFileChannel(file, "rw"); StringBuilder sb = new StringBuilder(); int size = 0; for (String s : data) { size += s.length() + 1; } CharBuffer cb = CharBuffer.allocate(size); for (String s : data) { cb.append(s); cb.append('\n'); } MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, cb.length()); mbb.put(cs.encode(cb)); fc.close(); } public static void write(Path file, int[] data) throws IOException { FileChannel fc = getFileChannel(file, "rw"); IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, data.length * 4).asIntBuffer(); for (int i : data) { ib.put(i); } fc.close(); } public static FileChannel getFileChannel(Path file, String mode) throws FileNotFoundException { if (!FILES.containsKey(file.toString())) { FILES.put(file.toString(), new RandomAccessFile(file.toString(), mode).getChannel()); } return FILES.get(file.toString()); } }