Java FileOutputStream Write Byte Array writeBytes(File file, byte[] bytes)

Here you can find the source of writeBytes(File file, byte[] bytes)

Description

write Bytes

License

Open Source License

Declaration

public static void writeBytes(File file, byte[] bytes)
            throws IOException 

Method Source Code

//package com.java2s;
/*//from   www  . ja va 2  s . c om
 * Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). 
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description:
 *
 */

import java.io.File;

import java.io.FileOutputStream;

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

import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.Writer;

public class Main {
    public static void writeBytes(File file, byte[] bytes)
            throws IOException {
        OutputStream out = null;
        try {
            out = new FileOutputStream(file);
            out.write(bytes, 0, bytes.length);
        } finally {
            close(out);
        }
    }

    /**
     * Closes the streams and resources.
     * 
     * @param resource
     *            to be closed.
     */
    public static void close(Object resource) {
        try {
            if (resource instanceof InputStream) {
                ((InputStream) resource).close();
            } else if (resource instanceof OutputStream) {
                ((OutputStream) resource).close();
            } else if (resource instanceof Reader) {
                ((Reader) resource).close();
            } else if (resource instanceof Writer) {
                ((Writer) resource).close();
            } else if (resource instanceof RandomAccessFile) {
                ((RandomAccessFile) resource).close();
            } else if (resource != null) {
                throw new IllegalArgumentException("Unknown resource: "
                        + resource);
            }
        } catch (IOException e) {
        }
    }
}

Related

  1. writeBytes(File f, byte[] data)
  2. writeBytes(File file, byte[] arr)
  3. writeBytes(File file, byte[] ba)
  4. writeBytes(File file, byte[] bytes)
  5. writeBytes(File file, byte[] bytes)
  6. writeBytes(File file, byte[] bytes)
  7. writeBytes(File file, byte[] bytes, boolean append)
  8. writeBytes(File file, byte[] data)
  9. writeBytes(File file, byte[] pattern, int repeat)