Java Text File Write nio write(final Map dataBase, final File currentFile)

Here you can find the source of write(final Map dataBase, final File currentFile)

Description

write

License

Open Source License

Declaration

public static void write(final Map<String, String> dataBase, final File currentFile) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Main {
    private static final int POS_STEP = 5;
    private static final int BUF_SIZE = 4096;

    public static void write(final Map<String, String> dataBase, final File currentFile) throws IOException {

        OutputStream currentStream = new FileOutputStream(currentFile);
        BufferedOutputStream bufferStream = new BufferedOutputStream(currentStream, BUF_SIZE);
        DataOutputStream dataStream = new DataOutputStream(bufferStream);
        long biasing = 0;

        for (String key : dataBase.keySet()) {
            biasing += key.getBytes(StandardCharsets.UTF_8).length + POS_STEP;
        }/*ww w  . ja  v  a  2s  .c o  m*/
        List<String> values = new ArrayList<String>(dataBase.keySet().size());
        for (String key : dataBase.keySet()) {
            String value = dataBase.get(key);
            values.add(value);
            dataStream.write(key.getBytes(StandardCharsets.UTF_8));
            dataStream.writeByte(0);
            dataStream.writeInt((int) biasing);
            biasing += value.getBytes(StandardCharsets.UTF_8).length;
        }

        for (String value : values) {
            dataStream.write(value.getBytes());
        }
        closeStream(dataStream);
    }

    private static void closeStream(final Closeable stream) throws IOException {
        stream.close();
    }
}

Related

  1. write(File file, List lines, StandardOpenOption... options)
  2. write(File file, String content)
  3. write(File file, String data, boolean append)
  4. write(final Object o, final String filename)
  5. write(final OutputStream outputStream, final String string)
  6. write(final StringBuffer data, final Writer output)
  7. write(OutputStream stream, String string)