|
|
|
|
|
|
/* ===========================================================
How to read ONE record USING the meta data
ONE record has n_fields fields according to the Meta Data
=========================================================== */
for ( int i = 0; i < n_fields; i++ )
{
/* --------------------------------------------------------------
HOW you access the i-th field depends on the TYPE of the field !
-------------------------------------------------------------- */
if ( dataDes[i].fieldType.equals("I") )
{
/* --------------------------------------------------------
Field i is an integer, use i_buf[i] to store the value
-------------------------------------------------------- */
i_buf[i] = file.ReadInt( ); // Store in an int type var
}
else
{
/* --------------------------------------------------------
Field i is an String, use c_buf[i] to store the value
-------------------------------------------------------- */
c_buf[i] = in.nextLine(); // Store in a String type variable
}
}
|
public static void main(String[] args) throws IOException
{
/* -----------------------------------------------------------
1. Find out the STRUCTURE of the data file
(and store this info !)
Read in the meta data from db-description
and store meta data in the DataDes[] array
----------------------------------------------------------- */
DataFile descrFile = new DataFile("db-description");
// Open the meta data file
n_fields = 0; // We count the actual number of fields in data
while ( true )
{
try
{
/* =======================================================
Read the description of the next field and store it
======================================================= */
dataDes[n_fields].fieldName = descrFile.ReadString(24);
dataDes[n_fields].fieldType = descrFile.ReadString(4);
dataDes[n_fields].fieldSize = descrFile.ReadInt();
n_fields++; // Remember the number of fields
System.out.println("Field: " + dataDes[n_fields].fieldName
+ ", type: " + dataDes[n_fields].fieldType
+ ", size: " + dataDes[n_fields].fieldSize);
}
catch ( IOException e )
{
System.out.println("\nFinish reading data description file....\n");
break; // Read error: no more data !!!
}
}
|
Demo:
cd /home/cs377001/demo/Phys-Data-Dependance/DB java ReadStuFile |
|