Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lesson12;
- import java.io.Serializable;
- public class Animal implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private String name;
- private int age;
- private transient String color;
- public Animal(String name, int age, String color) {
- this.name = name;
- this.age = age;
- this.color = color;
- }
- @Override
- public String toString() {
- return name + "-" + age + "-" + color;
- }
- }
- package Lesson12;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- public class Main {
- public static void main(String[] args) {
- Animal dog = new Animal("Arman", 5, "White");
- Animal cat = new Animal("Masha", 2, "Grey");
- Animal parrot = new Animal("Petya", 1, "Green");
- System.out.println("Dog: " + dog);
- System.out.println("Cat: " + cat);
- System.out.println("Parrot: " + parrot);
- try {
- File file = new File("vyvod.txt");
- FileOutputStream fos = new FileOutputStream(file);
- ObjectOutputStream ous = new ObjectOutputStream(fos);
- ous.writeObject(dog);
- ous.writeObject(cat);
- ous.writeObject(parrot);
- ous.close();
- fos.close();
- FileInputStream fis = new FileInputStream(file);
- ObjectInputStream ois = new ObjectInputStream(fis);
- cat = (Animal) ois.readObject();
- parrot = (Animal) ois.readObject();
- dog = (Animal) ois.readObject();
- ois.close();
- fis.close();
- System.out.println("Dog: " + dog);
- System.out.println("Cat: " + cat);
- System.out.println("Parrot: " + parrot);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement