Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 09b8e72

Browse files
Update README.md
1 parent 6884384 commit 09b8e72

File tree

1 file changed

+73
-79
lines changed

1 file changed

+73
-79
lines changed

‎README.md

Lines changed: 73 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2598,89 +2598,83 @@ Serialization is a mechanism of converting the state of an object into a byte st
25982598
25992599
```java
26002600
/**
2601-
* Serialization and Deserialization
2602-
* example of a Java object
2603-
*
2604-
**/
2605-
import java.io.*;
2606-
2607-
class Employee implements Serializable {
2608-
private static final long serialversionUID =
2609-
129348938L;
2610-
transient int a;
2611-
static int b;
2612-
String name;
2613-
int age;
2614-
2615-
// Default constructor
2616-
public Employee(String name, int age, int a, int b) {
2617-
this.name = name;
2618-
this.age = age;
2619-
this.a = a;
2620-
this.b = b;
2601+
* Serialization and Deserialization
2602+
*/
2603+
import java.io.*;
2604+
2605+
class Employee implements Serializable {
2606+
private static final long serialversionUID = 129348938L;
2607+
transient int a;
2608+
static int b;
2609+
String name;
2610+
int age;
2611+
2612+
// Default constructor
2613+
public Employee(String name, int age, int a, int b) {
2614+
this.name = name;
2615+
this.age = age;
2616+
this.a = a;
2617+
this.b = b;
26212618
}
2622-
}
2623-
2624-
public class SerialExample {
2619+
}
2620+
2621+
public class SerialExample {
2622+
2623+
public static void printdata(Employee object1) {
2624+
System.out.println("name = " + object1.name);
2625+
System.out.println("age = " + object1.age);
2626+
System.out.println("a = " + object1.a);
2627+
System.out.println("b = " + object1.b);
2628+
}
2629+
2630+
public static void main(String[] args) {
2631+
Employee object = new Employee("ab", 20, 2, 1000);
2632+
String filename = "file.txt";
2633+
2634+
// Serialization
2635+
try {
2636+
// Saving of object in a file
2637+
FileOutputStream file = new FileOutputStream(filename);
2638+
ObjectOutputStream out = new ObjectOutputStream(file);
2639+
2640+
// Method for serialization of object
2641+
out.writeObject(object);
2642+
2643+
out.close();
2644+
file.close();
26252645
2626-
public static void printdata(Employee object1) {
2627-
System.out.println("name = " + object1.name);
2628-
System.out.println("age = " + object1.age);
2629-
System.out.println("a = " + object1.a);
2630-
System.out.println("b = " + object1.b);
2631-
}
2632-
2633-
public static void main(String[] args) {
2634-
Employee object = new Employee("ab", 20, 2, 1000);
2635-
String filename = "shubham.txt";
2636-
2637-
// Serialization
2638-
try {
2639-
// Saving of object in a file
2640-
FileOutputStream file = new FileOutputStream(filename);
2641-
ObjectOutputStream out = new ObjectOutputStream(file);
2642-
2643-
// Method for serialization of object
2644-
out.writeObject(object);
2645-
2646-
out.close();
2647-
file.close();
2648-
26492646
System.out.println("Object has been serialized\n"
2650-
+ "Data before Deserialization.");
2651-
printdata(object);
2652-
// value of static variable changed
2653-
object.b = 2000;
2654-
}
2655-
catch (IOException ex) {
2656-
System.out.println("IOException is caught");
2657-
}
2658-
2659-
object = null;
2660-
2661-
// Deserialization
2662-
try {
2663-
// Reading the object from a file
2664-
FileInputStream file = new FileInputStream(filename);
2665-
ObjectInputStream in = new ObjectInputStream(file);
2666-
2667-
// Method for deserialization of object
2668-
object = (Employee)in.readObject();
2669-
2670-
in.close();
2671-
file.close();
2647+
+ "Data before Deserialization.");
2648+
printdata(object);
2649+
// value of static variable changed
2650+
object.b = 2000;
2651+
} catch (IOException ex) {
2652+
System.out.println("IOException is caught");
2653+
}
2654+
2655+
object = null;
2656+
2657+
// Deserialization
2658+
try {
2659+
// Reading the object from a file
2660+
FileInputStream file = new FileInputStream(filename);
2661+
ObjectInputStream in = new ObjectInputStream(file);
2662+
2663+
// Method for deserialization of object
2664+
object = (Employee) in.readObject();
2665+
2666+
in.close();
2667+
file.close();
26722668
System.out.println("Object has been deserialized\n"
2673-
+ "Data after Deserialization.");
2674-
printdata(object);
2675-
System.out.println("z = " + object1.z);
2676-
}
2677-
catch (IOException ex) {
2678-
System.out.println("IOException is caught");
2679-
}
2680-
catch (ClassNotFoundException ex) {
2681-
System.out.println("ClassNotFoundException is caught");
2682-
}
2683-
}
2669+
+ "Data after Deserialization.");
2670+
printdata(object);
2671+
System.out.println("z = " + object1.z);
2672+
} catch (IOException ex) {
2673+
System.out.println("IOException is caught");
2674+
} catch (ClassNotFoundException ex) {
2675+
System.out.println("ClassNotFoundException is caught");
2676+
}
2677+
}
26842678
}
26852679
```
26862680

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /