Source of ReadMatrix1.java


  1: 
  2: import java.io.*; 
  3: 
  4: public class ReadMatrix1 {
  5: 
  6:   static double[][] data;
  7: 
  8:   public static void main(String[] args) {
  9:     if (args.length > 0) {
 10:       try { 
 11:         FileInputStream in = new FileInputStream(args[0]); 
 12:         int row = readInt(in); 
 13:         System.out.println("row = " + row); 
 14:         int col = readInt(in);
 15:         System.out.println("col = " + col); 
 16:         data = new double[row][col]; 
 17:         for (int i = 0; i < row; i++) {
 18:           for (int j = 0; j < col; j++) {
 19:             data[i][j] = readDouble(in); 
 20:             System.out.println("data[" + i + "][" + j + "] = " + data[i][j]);  
 21:           }
 22:         }        
 23:       } catch (IOException e) {}  
 24:     }
 25:   }
 26: 
 27:   public static int readInt(InputStream in) 
 28:       throws IOException {
 29:     byte[] buf = new byte[4];
 30:     in.read(buf); 
 31:     int i = 0; 
 32:     for (int k = 0; k < 4; k++) {
 33:       i <<= 8; 
 34:       i += (((int) buf[k]) & 0xFF); 
 35:     }
 36:     return i; 
 37:   }
 38: 
 39:   public static double readDouble(InputStream in) 
 40:        throws IOException {
 41:     byte[] buf = new byte[8];
 42:     in.read(buf); 
 43:     long l = 0; 
 44:     for (int k = 0; k < 8; k++) {
 45:       l <<= 8; 
 46:       l += (((int) buf[k]) & 0xFF); 
 47:     }    
 48:     return Double.longBitsToDouble(l); 
 49:   } 
 50: }