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.

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

Declaration

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

Method Source Code

//package com.java2s;
/* This file is part of the Joshua Machine Translation System.
 * /*from  www  .j ava  2  s  .  com*/
 * Joshua is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

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

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

        FileOutputStream out = new FileOutputStream(filename);

        byte[] b = new byte[4];

        for (int word : data) {
            for (int i = 0; i < 4; i++) {
                int offset = (b.length - 1 - i) * 8;
                b[i] = (byte) ((word >>> offset) & 0xFF);
            }

            out.write(b);
        }
    }
}

Related

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