Java Text File Write by Charset write(Path file, CharBuffer data, Charset cs)

Here you can find the source of write(Path file, CharBuffer data, Charset cs)

Description

write

License

Open Source License

Declaration

public static void write(Path file, CharBuffer data, Charset cs) throws IOException 

Method Source Code


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

Related

  1. openWriter(OutputStream stream, Charset charset, boolean autoflush)
  2. write(char[] data, File file, String charsetName)
  3. write(File file, Charset charset, String content)
  4. write(File file, Object content, Charset cs, boolean append)
  5. write(final String s, final OutputStream out, Charset charset)
  6. write(Path path, Iterable lines, Charset cs, OpenOption... options)
  7. write(Path path, Iterable lines, Charset cs, OpenOption... options)
  8. write(String toWrite, File file, Charset charset)
  9. writeAll(Path path, String data, Charset cs)