Java File Save SaveIntFile(int[] list, String filename)

Here you can find the source of SaveIntFile(int[] list, String filename)

Description

Save the specified array of ints in the specified file IN PC FORMAT.

License

Open Source License

Parameter

Parameter Description
list The array of ints to save.
filename The name of the file to write.

Declaration

public static void SaveIntFile(int[] list, String filename) 

Method Source Code


//package com.java2s;
/* /*from   w  ww .j  av  a2s.c o m*/
 * File: FileUtil.java
 *
 * Copyright (C) 2010, Dennis Mikkelson
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 * Contact : Dennis Mikkelson <mikkelsond@uwstout.edu>
 *           Department of Mathematics, Statistics and Computer Science
 *           University of Wisconsin-Stout
 *           Menomonie, WI 54751, USA
 *
 * This work was supported by the Spallation Neutron Source Division
 * of Oak Ridge National Laboratory, Oak Ridge, TN, USA.
 *
 *  Last Modified:
 * 
 *  $Author:$
 *  $Date:$            
 *  $Revision:$
 */

import java.io.*;

public class Main {
    /**
     *  Save the specified array of ints in the specified file IN PC FORMAT.
     *
     *  @param list     The array of ints to save.
     *  @param filename The name of the file to write.
     */
    public static void SaveIntFile(int[] list, String filename) {
        if (list == null)
            throw new IllegalArgumentException("Array of ints is null");

        if (filename == null)
            throw new IllegalArgumentException("Filename String is NULL");

        byte[] buffer = new byte[4 * list.length];

        for (int i = 0; i < list.length; i++)
            setInt_32(list[i], buffer, 4 * i);

        try {
            RandomAccessFile r_file = new RandomAccessFile(filename, "rw");
            r_file.write(buffer);
            r_file.close();
        } catch (Exception ex) {
            System.out.println(ex);
            ex.printStackTrace();
            throw new IllegalArgumentException("Error writing file " + filename);
        }
    }

    /**
     * Encode the int value into a sequeence of fout bytes in the buffer
     * starting at position i.  The four bytes determining the int value 
     * are stored in buffer in the sequence: b0, ... b3, with the lowest 
     * order byte, b0, first and the the highest order byte, b3, last.
     *                    
     * @param value  The int value to be stored in four successive bytes
     *               of the buffer. 
     *
     * @param buffer The byte buffer where the bytes are to be stored.
     *
     * @param i      The index in the buffer where the first byte of the 
     *               int should stored. 
     */
    public static void setInt_32(int value, byte[] buffer, int i) {
        for (int count = 0; count < 4; count++) {
            buffer[i++] = (byte) (value & 0xFF);
            value = value >> 8;
        }
    }
}

Related

  1. saveFile(InputStream st, File testFile)
  2. saveFile(String fname, byte[] bytes)
  3. saveFile(String where)
  4. saveInputToOutput(InputStream is, OutputStream os)
  5. saveIntArray(int[] array, PrintStream out)
  6. saveStreamAsString(InputStream is)
  7. saveStringDoubleMap(Map map, int dim, PrintStream out)
  8. saveToInputStream(ByteArrayInputStream in, OutputStream out)
  9. saveTrecOneEntry(BufferedWriter trecFile, String topicId, String docId, int docPos, float score, String runId)