Java FileOutputStream Write saveToFile(final String file, final byte[] data, final boolean overwrite)

Here you can find the source of saveToFile(final String file, final byte[] data, final boolean overwrite)

Description

save byte array into a file

License

Open Source License

Parameter

Parameter Description
file file path
data data to save into a file
overwrite true if the existing file can be overwritten

Exception

Parameter Description
IOException an exception

Declaration

public static void saveToFile(final String file, final byte[] data, final boolean overwrite)
        throws IOException 

Method Source Code

//package com.java2s;
/**/*from w w  w .jav a 2  s  .  c o  m*/
 * Copyright (c) 2014 Netflix, Inc.  All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    /**
     * save byte array into a file
     *
     * @param file file path
     * @param data data to save into a file
     * @param overwrite true if the existing file can be overwritten
     * @throws IOException
     */
    public static void saveToFile(final String file, final byte[] data, final boolean overwrite)
            throws IOException {
        assertNotNull(file, "file");
        assertNotNull(data, "data");
        final File f = new File(file);
        if (f.exists() && !overwrite) {
            throw new IllegalArgumentException("cannot overwrite file " + file);
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(f);
            fos.write(data);
        } finally {
            if (fos != null)
                try {
                    fos.close();
                } catch (final IOException ignore) {
                }
        }
    }

    /**
     * @param param parameter
     * @param name parameter name
     */
    public static void assertNotNull(final Object param, final String name) {
        if (param == null)
            throw new IllegalArgumentException(String.format("NULL %s", (name != null) ? name : "parameter"));
    }
}

Related

  1. saveToDisk(InputStream is, String filename)
  2. saveToDisk(String contents, String filename)
  3. saveToEml(Message mail, File emlFile)
  4. saveToFile(byte[] data, String filename)
  5. saveToFile(File file, byte[] buffer)
  6. saveToFile(final String text, final File file)
  7. saveToFile(InputStream in, File dst)
  8. saveToFile(InputStream in, String fileName)
  9. saveToFile(InputStream stream, File file)