| import java.io.*;
class DataBaseWriter
{
public static void main (String S[])
{
String Name, Address;
int Age;
double Salary;
char Sex;
char Loop = 'Y';
while (Loop == 'Y')
{
System.out.println("Please supply the following
data...");
System.out.print ("Your name >");
Name = KeyInput.ReadString();
System.out.print ("Your address >");
Address = KeyInput.ReadString();
System.out.print ("Your age >");
Age = KeyInput.ReadInt();
System.out.print ("Your salary >");
Salary = KeyInput.ReadDouble();
System.out.print ("Are you [M]ale or [F]emale?
>");
Sex = KeyInput.ReadChar();
try
{
DataOutputStream DOS = new DataOutputStream (new FileOutputStream("MyDatabase.dat"));
DOS.writeInt(Age);
DOS.writeDouble(Salary);
DOS.writeChar(Sex);
DOS.writeInt(Name.length());
DOS.writeBytes(Name);
DOS.writeInt(Address.length());
DOS.writeBytes(Address);
System.out.println("The data was successfully stored
to disk");
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
System.out.println("Enter more data? [Y] / [N]");
Loop = KeyInput.ReadChar();
if (Loop == 'N')
{
break;
}
}
}
};
abstract class KeyInput
{
private static String Fetch()
{
BufferedReader In = new BufferedReader(new InputStreamReader(System.in));
String Temp;
try
{
Temp = In.readLine();
}
catch (IOException e)
{
Temp = "";
}
return Temp;
}
public static String ReadString()
{
return Fetch();
}
public static int ReadInt()
{
String Line = Fetch();
int iVal = 0;
try
{
iVal = Integer.parseInt(Line);
}
catch(NumberFormatException e)
{
}
return iVal;
}
public static double ReadDouble()
{
String Line = Fetch();
double dVal = 0.0;
try
{
dVal = Double.parseDouble(Line);
}
catch(NumberFormatException e)
{
}
return dVal;
}
static char ReadChar()
{
return Fetch().charAt(0);
}
};
|