Java Text File Write nio write(final Object o, final String filename)

Here you can find the source of write(final Object o, final String filename)

Description

write to gzip file.

License

Open Source License

Parameter

Parameter Description
o a parameter
filename a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void write(final Object o, final String filename) throws IOException 

Method Source Code

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

import java.io.BufferedWriter;

import java.io.File;
import java.io.IOException;

import java.io.OutputStreamWriter;
import java.nio.file.Files;

import java.util.zip.GZIPOutputStream;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**//from w w  w.  j av  a  2 s . com
     * write to gzip file.
     * @param o
     * @param filename
     * @throws IOException
     */
    public static void write(final Object o, final String filename) throws IOException {
        try (final BufferedWriter bw = setUpWriter(filename)) {
            bw.write(MAPPER.writeValueAsString(o));
            bw.close();
        }
    }

    /**
     * set up gzip file writer.
     * @param filename
     * @return
     * @throws IOException
     */
    private static BufferedWriter setUpWriter(final String filename) throws IOException {
        return new BufferedWriter(
                new OutputStreamWriter(new GZIPOutputStream(Files.newOutputStream(new File(filename).toPath()))));
    }
}

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 Map dataBase, final File currentFile)
  5. write(final OutputStream outputStream, final String string)
  6. write(final StringBuffer data, final Writer output)
  7. write(OutputStream stream, String string)
  8. write(String content, String file)