Skip to content

Instantly share code, notes, and snippets.

@yassaa
Created November 16, 2014 13:55
Show Gist options
  • Save yassaa/385d315ab69559dfee62 to your computer and use it in GitHub Desktop.
Save yassaa/385d315ab69559dfee62 to your computer and use it in GitHub Desktop.
Userdatabase
package test_Bookingsystem;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class UserDatabase {
//write to file
public void getUsers (String Name, String Email, long Phonenumber){
//DataOutputStream converts primitive-type values or strings into bytes and
//outputs the bytes to the stream.
try(DataOutputStream output = new DataOutputStream(new FileOutputStream("UserDatabase1.dat"))){
//try statements - try writing all objects to file
output.writeUTF(Name);
output.writeUTF(Email);
output.writeLong(Phonenumber);
}
//adding catch blocks
catch (IOException ex) {
System.out.println(" .... ");
}
}
public void saveUsers(User user ){
//DataInputStream reads bytes from the stream and converts them into
//appropriate primitive-type values or strings.
try(DataInputStream input = new DataInputStream(new FileInputStream("UserDatabase1_email.dat"))){
//try statements - try reading all objects to file
System.out.println("User Name: " + input.readUTF());
System.out.println("Email: " + input.readUTF());
System.out.println("Phonenumber: " + input.readLong());
}
//adding catch blocks
catch (IOException ex) {
System.out.println("....");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment