import java.io.*; import java.util.Scanner; class DataDescription { String fieldName; String fieldType; int fieldSize; } public class ReadMetaData { static int MAXNFIELDS = 10; // Max. # fields in 1 record static int n_fields; // Actual number of fields /* ------------------------------------------------------------ (1) Variables used to store the DESCRIPTION of the data ------------------------------------------------------------ */ static DataDescription[] dataDes = new DataDescription[MAXNFIELDS]; // This is a CONSTRUCTOR method for the static variable dataDes static { // Need to create objects for the dataDes[] for ( int i = 0; i < MAXNFIELDS; i++ ) { dataDes[i] = new DataDescription(); } } public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); /* =========================================================== We must first find out the STRUCTURE of the data file This information is stored in the data DESCRIPTION file =========================================================== */ DataFile descrFile = new DataFile("db-description"); // 1. Open the data description file /* ------------------------------------------------------- Read in the data description and store them in the DataDes[] array (define in (1)) ------------------------------------------------------- */ n_fields = 0; // Count the actual number of fields in data while ( true ) { try { dataDes[n_fields].fieldName = descrFile.ReadString(24); dataDes[n_fields].fieldType = descrFile.ReadString(4); dataDes[n_fields].fieldSize = descrFile.ReadInt(); System.out.println("Read an entry: " + dataDes[n_fields].fieldName + ", type: " + dataDes[n_fields].fieldType + ", size: " + dataDes[n_fields].fieldSize); n_fields++; } catch ( IOException e ) { System.out.println("\nFinish reading data description file....\n"); break; // Read error: no more data !!! } } /* ======================================================== Done... Show what we have read in ======================================================== */ for ( int i = 0; i < n_fields; i++ ) { System.out.println("dataDes[" + i + "] = " + dataDes[i].fieldName + "\t" + dataDes[i].fieldType + "\t" + dataDes[i].fieldSize ); } } }