Java RandomAccessFile Read load(String fn, int type, int m, int n)

Here you can find the source of load(String fn, int type, int m, int n)

Description

read a matrix from a binary file.

License

Open Source License

Parameter

Parameter Description
fn the file name
type data type: DT_BYTE, DT_SHORT, DT_INT, DT_FLOAT, DT_DOUBLE
m number of rows (determined by file size if <= 0)
n number of columns (determined by file size if <=0)
skip number of bytes to be skipped from the beginning

Declaration

public static double[][] load(String fn, int type, int m, int n) throws IOException 

Method Source Code

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

import java.io.IOException;

import java.io.RandomAccessFile;

public class Main {
    /** binary file of unsigned bytes */
    public final static int DT_BYTE = 1;
    /** binary file of signed short integers (16-bits) */
    public final static int DT_SHORT = 2;
    /** binary file of signed integers (32-bits) */
    public final static int DT_INT = 3;
    /** binary file in IEEE floating format (32-bits) */
    public final static int DT_FLOAT = 4;
    /** binary file in IEEE floating format (64-bits) */
    public final static int DT_DOUBLE = 5;
    private static int[] dt_size = new int[] { 0, 1, 2, 4, 4, 8 };

    /** read a matrix from a binary file.  Note at least one dimension should
    * be positive//from  ww w .  ja  v  a 2  s  .co m
    * @param fn      the file name
    * @param type    data type: DT_BYTE, DT_SHORT, DT_INT, DT_FLOAT, DT_DOUBLE
    * @param m       number of rows (determined by file size if <= 0)
    * @param n       number of columns (determined by file size if <=0)
    * @param skip    number of bytes to be skipped from the beginning
    */
    public static double[][] load(String fn, int type, int m, int n) throws IOException {

        if (m <= 0 && n <= 0)
            return null;

        RandomAccessFile file = new RandomAccessFile(fn, "r");
        long l = file.length();
        if (l < 0) {
            file.close();
            return null;
        }

        l /= dt_size[type];
        if (m <= 0)
            m = (int) (l / n);
        else if (n <= 0)
            n = (int) (l / m);
        if (m <= 0 || n <= 0) {
            file.close();
            return null;
        }

        double[][] x = new double[m][n];

        /* file is default saved by columns */
        switch (type) {
        case DT_BYTE: /* Java byte is signed!! */
            for (int j = 0; j < n; j++)
                for (int i = 0; i < m; i++)
                    x[i][j] = (double) (0xff & (int) file.readByte());
            break;
        case DT_SHORT:
            for (int j = 0; j < n; j++)
                for (int i = 0; i < m; i++)
                    x[i][j] = (double) file.readShort();
            break;
        case DT_INT:
            for (int j = 0; j < n; j++)
                for (int i = 0; i < m; i++)
                    x[i][j] = (double) file.readInt();
            break;
        case DT_FLOAT:
            for (int j = 0; j < n; j++)
                for (int i = 0; i < m; i++)
                    x[i][j] = (double) file.readFloat();
            break;
        case DT_DOUBLE:
            for (int j = 0; j < n; j++)
                for (int i = 0; i < m; i++)
                    x[i][j] = file.readDouble();
            break;
        default:
            file.close();
            throw new IllegalArgumentException("Illegal data type");
        }
        file.close();

        return x;
    }
}

Related

  1. appendFile(RandomAccessFile main, RandomAccessFile extra)
  2. load(String f)
  3. LoadDspaceMapFile(String filename)
  4. LoadFloatFile(String filename)
  5. LoadGhostMapFile(String filename, int n_ids, int n_ghosts)
  6. read_arraydesc(final RandomAccessFile raf)