diff --git a/Java/BasicSerializationExample.java b/Java/BasicSerializationExample.java new file mode 100644 index 0000000..b57368a --- /dev/null +++ b/Java/BasicSerializationExample.java @@ -0,0 +1,58 @@ +/** + * Created by Руслан on 30.05.2017. + */ +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.Date; + + +public class BasicSerializationExample { + static final String file = "user.txt"; + + static void serialize(User user) { + try { + FileOutputStream fos = new FileOutputStream(file); + ObjectOutputStream outputStream = new ObjectOutputStream(fos); + outputStream.writeObject(user); + outputStream.close(); + } catch (IOException ex) { + System.err.println(ex); + } + } + + static User deserialize() { + User savedUser = null; + + try { + FileInputStream fis = new FileInputStream(file); + ObjectInputStream inputStream = new ObjectInputStream(fis); + savedUser = (User) inputStream.readObject(); + inputStream.close(); + } catch (IOException | ClassNotFoundException ex) { + System.err.println(ex); + } + + return savedUser; + } + + public static void main(String[] args) { + String username = "ruslan"; + String email = "ruslankobrin@gmail.com"; + String password = "pass"; + Date birthDay = new Date(); + int age = 30; + + User newUser = new User(username, email, password, birthDay, age); + + serialize(newUser); + + User deseriaUser = deserialize(); + + if (deseriaUser != null) { + deseriaUser.printInfo(); + } + } +} \ No newline at end of file diff --git a/Java/User.java b/Java/User.java new file mode 100644 index 0000000..2fc37fb --- /dev/null +++ b/Java/User.java @@ -0,0 +1,35 @@ +/** + * Created by Руслан on 30.05.2017. + */ +import java.io.Serializable; +import java.util.Date; + +public class User implements Serializable { + + + private String username; + private String email; + private String password; + private Date birthday; + private int age; + + public User(String username, String email, String password, Date birthday, + int age) { + this.username = username; + this.email = email; + this.password = password; + this.birthday = birthday; + this.age = age; + } + + public void printInfo() { + System.out.println("username: " + username); + System.out.println("email: " + email); + System.out.println("password: " + password); + System.out.println("birthday: " + birthday); + System.out.println("age: " + age); + } + + // getters and setters + +} \ No newline at end of file
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: