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

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

Description

Writes bytes to the specified file

License

Open Source License

Parameter

Parameter Description
file The file to write the bytes to.
bytes The bytes to write.

Exception

Parameter Description
IOException When writing to the file failed.

Declaration


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

Method Source Code


//package com.java2s;
/*/*w w w . j  a  v  a  2 s.  c  o m*/
 * Copyright (C) 2010 Klaus Reimer <k@ailis.de>
 * See LICENSE.md for licensing information.
 */

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**
     * Writes bytes to the specified file
     *
     * @param file
     *            The file to write the bytes to.
     * @param bytes
     *            The bytes to write.
     * @throws IOException
     *             When writing to the file failed.
     */

    public static void writeBytes(final File file, final byte[] bytes) throws IOException {
        try (final ByteArrayInputStream input = new ByteArrayInputStream(bytes);
                final FileOutputStream output = new FileOutputStream(file)) {
            copy(input, output);
        }
    }

    /**
     * Copies data from the input stream to the output stream.
     *
     * @param input
     *            The input stream.
     * @param output
     *            The output stream
     * @throws IOException
     *             When copying failed.
     */

    public static void copy(final InputStream input, final OutputStream output) throws IOException {
        final byte[] buffer = new byte[8192];
        int read;

        while ((read = input.read(buffer)) > 0) {
            output.write(buffer, 0, read);
        }
    }
}

Related

  1. writeBytes(File file, byte[] data)
  2. writeBytes(File file, byte[] pattern, int repeat)
  3. writeBytes(File file, byte[] source, int offset, int len)
  4. writeBytes(File filename, byte[] contents)
  5. writeBytes(File outputFile, byte[] bytes)
  6. writeBytes(final File file, final byte[] bytes)
  7. writeBytes(int[] data, String filename)
  8. writeBytes(int[] data, String filename)
  9. writeBytes(String filename, byte[] bytes)