Hello, I have taken some fragments of a code, and I do not understand these fragments, could you explain to me what function they fulfill? Thank you...
1-
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
two-
public String toString() {
return (getNombre() + " " + getP_Apellido() + " "
+ getS_Apellido());
}
3-
public static void main(String[] args) {
Employee e = new Employee();
e.name = "Reyan Ali";
e.address = "Phokka Kuan, Ambehta Peer";
e.SSN = 11122333;
e.number = 101;
try {
FileOutputStream fileOut
= new FileOutputStream("employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /employee.ser");
} catch (IOException i) {
i.printStackTrace();
}
}
4-
public void mailCheck() {
System.out.println("Mailing a check to " + name
+ " " + address);
}
5-
Employee e = null;
try {
FileInputStream fileIn = new FileInputStream("employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
return;
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");
System.out.println("Name: " + e.name);
System.out.println("Address: " + e.address);
System.out.println("SSN: " + e.SSN);
System.out.println("Number: " + e.number);
}
I would be grateful if you could explain to me what each code does, they are fragments of a complete code that I do not understand, thank you very much in advance...
1-
This code is part of a try catch statement. This instruction is used to catch exceptions and thus avoid an inappropriate behavior of our program. You can specify the type of exception you want to catch, in this case FileNotFoundException (happens if the file we are referring to cannot be found) and IOException (happens when we have a problem when reading or writing).
An exception can be caught without indicating the type of exception, doing so in a general way with Exception.
http://leo.ugr.es/J2ME/CLDC/transjava/node10.html
two-
Definition of the method toString(); All objects have a toStrnig method defined. What has been done here is to redefine it so that, when called, it returns a string made up of a first name last name last name. This is usually done when we create a custom object, to return whatever we want.
3-
4-
The mailCheck method is defined, which what it does is paint the Mailing check to name address message on the screen, replacing name and address with their corresponding values
5-
This part of code is explained with what was said in the previous ones.