0 added
0 removed
Original
2026-01-01
Modified
2026-02-26
1
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Map; public class App { public static void main(String[] args) throws IOException, ClassNotFoundException { class Student implements Serializable { String name; int age; Map<String, String> tel; public Student(String name, int age, Map<String, String> tel) { this.name = name; this.age = age; this.tel = tel; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", tel=" + tel + '}'; } } Student student = new Student("Ivan", 25, Map.of("mob", "123-45-67")); FileOutputStream fileOutputStream = new FileOutputStream("file.txt"); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(student); objectOutputStream.close(); // Десериализуем объект и выведем его для проверки FileInputStream fileInputStream = new FileInputStream("file.txt"); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); Student student1 = (Student) objectInputStream.readObject(); System.out.println(student1); // => Student{name='Ivan', age=25, tel={mob=123-45-67}} objectInputStream.close(); } }
1
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Map; public class App { public static void main(String[] args) throws IOException, ClassNotFoundException { class Student implements Serializable { String name; int age; Map<String, String> tel; public Student(String name, int age, Map<String, String> tel) { this.name = name; this.age = age; this.tel = tel; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", tel=" + tel + '}'; } } Student student = new Student("Ivan", 25, Map.of("mob", "123-45-67")); FileOutputStream fileOutputStream = new FileOutputStream("file.txt"); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(student); objectOutputStream.close(); // Десериализуем объект и выведем его для проверки FileInputStream fileInputStream = new FileInputStream("file.txt"); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); Student student1 = (Student) objectInputStream.readObject(); System.out.println(student1); // => Student{name='Ivan', age=25, tel={mob=123-45-67}} objectInputStream.close(); } }