Java FileOutputStream Write saveFileBinary(final File file, final byte[] data)

Here you can find the source of saveFileBinary(final File file, final byte[] data)

Description

Saves a data in a file.

License

Open Source License

Parameter

Parameter Description
file File to save the data in.
data Data to save in the file.

Exception

Parameter Description
IOException File exception occurred.

Declaration

public static void saveFileBinary(final File file, final byte[] data) throws IOException 

Method Source Code


//package com.java2s;
/* This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
 *
 * "Hilbert II" is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *///  w ww  .  ja va 2s  .  c om

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

import java.io.Reader;

import java.io.Writer;

public class Main {
    /**
     * Saves a <code>data</code> in a file. Existing files are overwritten.
     *
     * @param   file        File to save the data in.
     * @param   data        Data to save in the file.
     * @throws  IOException File exception occurred.
     */
    public static void saveFileBinary(final File file, final byte[] data) throws IOException {
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        try {
            out.write(data);
        } finally {
            out.close();
        }
    }

    /**
     * Closes input stream without exception.
     *
     * @param   in  Input stream, maybe <code>null</code>.
     */
    public static void close(final InputStream in) {
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) {
                // ignore
            }
        }
    }

    /**
     * Closes writer without exception.
     *
     * @param   writer  Writer, maybe <code>null</code>.
     */
    public static void close(final Writer writer) {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception e) {
                // ignore
            }
        }
    }

    /**
     * Closes out stream without exception.
     *
     * @param   out Output stream, maybe <code>null</code>.
     */
    public static void close(final OutputStream out) {
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
                // ignore
            }
        }
    }

    /**
     * Closes input reader without exception.
     *
     * @param   reader  Reader, maybe <code>null</code>.
     */
    public static void close(final Reader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception e) {
                // ignore
            }
        }
    }
}

Related

  1. saveFile(String path, byte[] content)
  2. saveFile(String pathFile, String data)
  3. saveFile(String pathname, String fileName, String contents)
  4. saveFile(String pathRoot, String subPath, boolean isFoler, InputStream in)
  5. saveFile(String text, File saveFile)
  6. SaveFileFromInputStream(InputStream in, String fileName, String path)
  7. SaveFileFromInputStream(InputStream stream, String path, String filename)
  8. saveInFile(String filename, String content, String charsetName)
  9. saveInputStream(InputStream _input_stream, File _file)