Java FileOutputStream Write Byte Array writeBytes(int[] data, String filename)

Here you can find the source of writeBytes(int[] data, String filename)

Description

Writes data from the integer array to disk as raw bytes, overwriting the old file if present.

License

Open Source License

Parameter

Parameter Description
data The integer array to write to disk.
filename The filename where the data should be written.

Exception

Parameter Description
IOException an exception

Return

the FileOutputStream on which the bytes were written

Declaration

public static FileOutputStream writeBytes(int[] data, String filename) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

public class Main {
    /**/*from www.j a  v a 2 s  . c o m*/
     * Writes data from the integer array to disk as raw bytes, overwriting the old file if present.
     * 
     * @param data The integer array to write to disk.
     * @param filename The filename where the data should be written.
     * @throws IOException
     * @return the FileOutputStream on which the bytes were written
     */
    public static FileOutputStream writeBytes(int[] data, String filename) throws IOException {
        FileOutputStream out = new FileOutputStream(filename, false);
        writeBytes(data, out);
        return out;
    }

    /**
     * Writes data from the integer array to disk as raw bytes.
     * 
     * @param data The integer array to write to disk.
     * @param out The output stream where the data should be written.
     * @throws IOException
     */
    public static void writeBytes(int[] data, OutputStream out) throws IOException {

        byte[] b = new byte[4];

        for (int word : data) {
            b[0] = (byte) ((word >>> 24) & 0xFF);
            b[1] = (byte) ((word >>> 16) & 0xFF);
            b[2] = (byte) ((word >>> 8) & 0xFF);
            b[3] = (byte) ((word >>> 0) & 0xFF);

            out.write(b);
        }
    }
}

Related

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