JAVAPRO_IR Telegram 4437
🚀 آموزش Serialization در جاوا

🔹 در واقع Serialization فرایندی است که در آن یک شیء (Object) به یک جریان بایت (Byte Stream) تبدیل می‌شود تا بتوان آن را:

* در یک فایل ذخیره کرد 🗂️
* از طریق شبکه ارسال کرد 🌐

🔹و Deserialization عکس این فرایند است؛ یعنی تبدیل جریان بایت به شیء.


پیاده‌سازی Serialization

برای Serializable کردن یک کلاس، کافیست کلاس را از `Serializable` پیاده‌سازی کنیم:


import java.io.*;

// کلاس مدل دانشجو
class Student implements Serializable {
private String name;
private int age;

// سازنده
public Student(String name, int age) {
this.name = name;
this.age = age;
}

// متد برای چاپ اطلاعات
public void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}

public class Main {
public static void main(String[] args) {
// شیء برای ذخیره
Student s1 = new Student("Ali", 22);

// Serialization: ذخیره در فایل
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student.ser"))) {
out.writeObject(s1);
System.out.println(" Object serialized successfully!");
} catch (IOException e) {
e.printStackTrace();
}

// Deserialization: خواندن از فایل
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("student.ser"))) {
Student s2 = (Student) in.readObject();
System.out.println(" Object deserialized successfully!");
s2.display();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}



🔎 نکات مهم:

1. برای Serialization نیازی به تعریف متد خاصی نداریم، فقط باید کلاس `implements Serializable` باشد.
2. اگر بخواهیم یک فیلد ذخیره نشود، از `transient` استفاده می‌کنیم.
3. فایل خروجی معمولا پسوند .ser دارد، ولی اجباری نیست.


💡 کاربرد واقعی:

* ذخیره وضعیت یک برنامه (مثلا Session)
* ارسال اشیاء بین کلاینت و سرور در شبکه

#کاربرـحرفهـای


🆔 @javapro_ir
🆔 @group_javapro
👍71



tgoop.com/javapro_ir/4437
Create:
Last Update:

🚀 آموزش Serialization در جاوا

🔹 در واقع Serialization فرایندی است که در آن یک شیء (Object) به یک جریان بایت (Byte Stream) تبدیل می‌شود تا بتوان آن را:

* در یک فایل ذخیره کرد 🗂️
* از طریق شبکه ارسال کرد 🌐

🔹و Deserialization عکس این فرایند است؛ یعنی تبدیل جریان بایت به شیء.


پیاده‌سازی Serialization

برای Serializable کردن یک کلاس، کافیست کلاس را از `Serializable` پیاده‌سازی کنیم:


import java.io.*;

// کلاس مدل دانشجو
class Student implements Serializable {
private String name;
private int age;

// سازنده
public Student(String name, int age) {
this.name = name;
this.age = age;
}

// متد برای چاپ اطلاعات
public void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}

public class Main {
public static void main(String[] args) {
// شیء برای ذخیره
Student s1 = new Student("Ali", 22);

// Serialization: ذخیره در فایل
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student.ser"))) {
out.writeObject(s1);
System.out.println(" Object serialized successfully!");
} catch (IOException e) {
e.printStackTrace();
}

// Deserialization: خواندن از فایل
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("student.ser"))) {
Student s2 = (Student) in.readObject();
System.out.println(" Object deserialized successfully!");
s2.display();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}



🔎 نکات مهم:

1. برای Serialization نیازی به تعریف متد خاصی نداریم، فقط باید کلاس `implements Serializable` باشد.
2. اگر بخواهیم یک فیلد ذخیره نشود، از `transient` استفاده می‌کنیم.
3. فایل خروجی معمولا پسوند .ser دارد، ولی اجباری نیست.


💡 کاربرد واقعی:

* ذخیره وضعیت یک برنامه (مثلا Session)
* ارسال اشیاء بین کلاینت و سرور در شبکه

#کاربرـحرفهـای


🆔 @javapro_ir
🆔 @group_javapro

BY برنامه نویسی جاوا | Java


Share with your friend now:
tgoop.com/javapro_ir/4437

View MORE
Open in Telegram


Telegram News

Date: |

Telegram Channels requirements & features The channel also called on people to turn out for illegal assemblies and listed the things that participants should bring along with them, showing prior planning was in the works for riots. The messages also incited people to hurl toxic gas bombs at police and MTR stations, he added. Just as the Bitcoin turmoil continues, crypto traders have taken to Telegram to voice their feelings. Crypto investors can reduce their anxiety about losses by joining the “Bear Market Screaming Therapy Group” on Telegram. "Doxxing content is forbidden on Telegram and our moderators routinely remove such content from around the world," said a spokesman for the messaging app, Remi Vaughn. Write your hashtags in the language of your target audience.
from us


Telegram برنامه نویسی جاوا | Java
FROM American