Java FileOutputStream Write saveFile(final byte[] bytes, final File target)

Here you can find the source of saveFile(final byte[] bytes, final File target)

Description

save an array of bytes

License

Open Source License

Parameter

Parameter Description
bytes a parameter
target a parameter

Declaration

public static void saveFile(final byte[] bytes, final File target) 

Method Source Code

//package com.java2s;
/**//from  w ww .jav  a2 s.co m
 * This file is part of Atomic Tagging.
 * 
 * Atomic Tagging 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 3 of the License, or (at your option) any later
 * version.
 * 
 * Atomic Tagging 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.
 * 
 * You should have received a copy of the GNU General Public License along with Atomic Tagging. If not, see
 * <http://www.gnu.org/licenses/>.
 */

import java.io.File;

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

public class Main {
    /**
     * save an array of bytes
     * 
     * @param bytes
     * @param target
     */
    public static void saveFile(final byte[] bytes, final File target) {
        if (!target.exists()) {
            try {
                if (!target.createNewFile()) {
                    throw new IllegalArgumentException(
                            "Can't write to given target file: " + target.getAbsolutePath());
                }
            } catch (final IOException e) {
                throw new IllegalArgumentException("Can't write to given target file: " + target.getAbsolutePath(),
                        e);
            }
        }

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(target);
        } catch (final FileNotFoundException ignore) {
            // Was checked previously.
        }

        if (fos == null) {
            throw new RuntimeException("Failed to create file streams.");
        }

        try {

            fos.write(bytes, 0, bytes.length);

        } catch (final IOException e) {
            // FIXME
            throw new RuntimeException("Failed to copy file.", e);
        } finally {
            try {
                fos.close();
            } catch (final IOException e) {
                // Nothing we can do, or is there?
                e.printStackTrace();
            }
        }
    }
}

Related

  1. saveFile(File f, String fileName, File desDir)
  2. saveFile(File file, byte[] contenido)
  3. saveFile(File file, InputStream is)
  4. saveFile(File file, String fileName, String filesDirectory)
  5. saveFile(File file, String savePath)
  6. saveFile(final File file, final byte[] dataToSave)
  7. saveFile(final File file, final String contents, final String encoding)
  8. saveFile(final InputStream in, final String path, final String fileName)
  9. saveFile(final String filename, final String s1, final String textOut, final String s3)