hi everyone i had been working on a project using random files (.dat) and now i want to change it to use a database instead. the thing is my project is quite long and it would be a hassle to go and change code.
i had used hash tables to store data and i just flused it into my randomfile. here is my code to read and write data. any suggestions on changing it to database.
thanks in advance
static void writeFiles(){
try{
FileOutputStream outStream;
ObjectOutputStream ooStream;
outStream = new FileOutputStream("CustomerList.dat");
ooStream = new ObjectOutputStream(outStream);
ooStream.writeObject(CusList);
ooStream.flush();
outStream.close();
outStream = new FileOutputStream("ProductList.dat");
ooStream = new ObjectOutputStream(outStream);
ooStream.writeObject(ProdList);
ooStream.flush();
outStream.close();
outStream = new FileOutputStream("SalesmanList.dat");
ooStream = new ObjectOutputStream(outStream);
ooStream.writeObject(sleList);
ooStream.flush();
outStream.close();
outStream = new FileOutputStream("Transaction_Products.dat");
ooStream = new ObjectOutputStream(outStream);
ooStream.writeObject(tpList);
ooStream.flush();
outStream.close();
outStream = new FileOutputStream("Temp.dat");
ooStream = new ObjectOutputStream(outStream);
ooStream.writeObject(tempList);
ooStream.flush();
outStream.close();
System.out.println("files saved");
}
catch (IOException exc){System.out.println("Error writing file!");}
}
static void readFiles(){
try{
FileInputStream inStream;
ObjectInputStream oiStream;
try{
inStream = new FileInputStream("ProductList.dat");
oiStream = new ObjectInputStream(inStream);
ProdList = (ProductList)oiStream.readObject();
}
catch(FileNotFoundException exc){
System.out.println("prodlist not found.");
}
try{
inStream = new FileInputStream("SalesmanList.dat");
oiStream = new ObjectInputStream(inStream);
sleList = (SalesmanList)oiStream.readObject();
}
catch(FileNotFoundException exc){
System.out.println("salelist not found.");
}
try{
inStream = new FileInputStream("Transaction_Products.dat");
oiStream = new ObjectInputStream(inStream);
tpList = (TPList)oiStream.readObject();
}
catch(FileNotFoundException exc){
System.out.println("tplist not found.");
}
inStream = new FileInputStream("CustomerList.dat");
oiStream = new ObjectInputStream(inStream);
CusList = (CustomerList)oiStream.readObject();
System.out.println("files reloaded.");
}
catch (FileNotFoundException exc){
System.out.println("File/s not found!");
writeMisc();
}
catch(IOException exc){
System.out.println("Error reading file! Creating new database.");
writeMisc();
}
catch(ClassNotFoundException exc){
System.out.println("Class not found.");
}
}
//read/write miscellaneous files
static void writeMisc(){
String settings = Premier_Customer.getCount()+" ";
String settings2 = Prod.getCount()+" ";
String settings3 = Salesman.getCount()+" ";
try{
FileWriter outStream = new FileWriter("misc.inf");
outStream.write(settings+"\n");
outStream.write(settings2+"\n");
outStream.write(settings3);
outStream.close();
System.out.println("miscellaneous files saved.");
}
catch(IOException exc){System.out.println("error writing misc file.");}
}
static void readMisc(){
try{
BufferedReader inStream = new BufferedReader(new FileReader("misc.inf"));
int setThis = Integer.parseInt(inStream.readLine().trim());
Premier_Customer.setInitialCount(setThis);
Prod.setInitialCount(setThis);
Salesman.setInitialCount(setThis);
System.out.println("miscellaneous files reloaded.");
}
catch(IOException exc){
System.out.println("Error reading misc file");
writeMisc();
}
}