Csc 1302, Honors Principles of Computer Science II (Fall 2016)
Week 3 (8 September 2016)
Initialize Database from file
During class, you will complete coding of the initializeDatabase method. The pseudo code is given below. You will handin Database.java by end of class time.- Initialize Database (Database.java)
//Create the database object by reading data from several files in directory dir public void initializeDatabase(String dir) { //Pseudo Code follows Open file "catalog.dat" Read number of relations in the database for each relation { // Read relation name and schema information Read relation name Read number of attributes un the relation Create empty array lists for attributes and domain for each attribute { Read attribute name Read attribute domain Add attribute name to attributes array list Add domain to domain array list } Create a new Relation object // Now Read data for tuples, create tuples and add to relation Construct file name for relation data file Open the relation data file Read number of tuples in relation for each tuple { Create new tuple object for each component of tuple { Read component value and add to tuple } Add tuple to relation } Close relation data file Add relation to database } Close catalog.dat file }
Here is a sample of files available in the directory: drinks, (also in .zip format: drinks.zip). The file catalog.dat contains schema information for all the relations in the database and individual .dat contain the relation instances (tuples).To read data from a file, you can use the following code:
FileInputStream fin1 = null; BufferedReader infile1 = null; try { fin1 = new FileInputStream("f.dat"); infile1 = new BufferedReader(new InputStreamReader(fin1)); String s = infile1.readLine(); ... ... fin1.close(); } catch (IOException e) { System.out.println("Error reading file"); }