import java.io.*; public class WriteMatrix1 { static double[][] data = { { Math.exp(2.0), Math.exp(3.0), Math.exp(4.0) }, { Math.exp(-2.0), Math.exp(-3.0), Math.exp(-4.0) }, }; public static void main(String[] args) { int row = data.length; int col = data[0].length; int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { System.out.println("data[" + i + "][" + j + "] = " + data[i][j]); } } if (args.length > 0) { try { FileOutputStream out = new FileOutputStream(args[0]); writeInt(row, out); writeInt(col, out); for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { writeDouble(data[i][j], out); } } out.close(); } catch (IOException e) {} } } public static void writeInt(int i, OutputStream out) throws IOException { byte[] buf = new byte[4]; for (int k = 3; k >= 0; k--) { buf[k] = (byte)(i & 0xFF); i >>>= 8; } out.write(buf); } public static void writeDouble(double d, OutputStream out) throws IOException { byte[] buf = new byte[8]; long l = Double.doubleToLongBits(d); for (int k = 7; k >= 0; k--) { buf[k] = (byte)(l & 0xFF); l >>>= 8; } out.write(buf); } }