|
|
|
class DataDescription
{
String fieldName;
String fieldType;
int fieldSize;
}
public class LookupData
{
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 static variables
static
{
// Need to create objects for the dataDes[]
for ( int i = 0; i < MAXNFIELDS; i++ )
{
dataDes[i] = new DataDescription();
}
}
/* -------------------------------------------------------
(2) Variables used to store the ACTUAL data
------------------------------------------------------- */
static String[] c_buf=new String[MAXNFIELDS]; // Used to store String fields
static int[] i_buf=new int[MAXNFIELDS]; // Used to store int fields
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
=========================================================== */
/* -------------------------------------------------------
Read in the Meta Data and store them in the
DataDes[] array (define in (1))
------------------------------------------------------- */
DataFile descrFile = new DataFile("db-description");
// 1. Open the data description file
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("Field: " + 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 !!!
}
}
/* ============================================================
Here we read the data and print them out...
============================================================ */
DataFile dataFile = new DataFile("db-data"); // First open data file
System.out.println( "The data file contains these records:\n");
while ( true )
{
/* ============================================================
Read in all the (n_fields) fields
============================================================ */
try
{
for ( int i = 0; i < n_fields; i++ )
{
/* ------------------------------------------------------
Read in the data depending on the TYPE of this field
------------------------------------------------------ */
if ( dataDes[i].fieldType.equals("I") )
{
/* --------------------------------------------------------
Field i is an integer, use i_buf[i] to store the value
-------------------------------------------------------- */
i_buf[i] = dataFile.ReadInt();
}
else
{
/* --------------------------------------------------------
Field i is an String, use c_buf[i] to store the value
-------------------------------------------------------- */
c_buf[i] = dataFile.ReadString( dataDes[i].fieldSize );
// We know the filed size !!!
}
}
PrintRecord(); // Print the data read - see below for method
}
catch ( IOException e )
{
System.out.print("\nDone.\n");
break;
}
}
|
How to run the program:
|
Demo:
cd /home/cs377001/demo/Phys-Data-Dependance/DB java LookupData |