Created
July 19, 2016 11:57
-
-
Save sunmeat/b8d5fb5db9f59944dd0da1adcda3f400 to your computer and use it in GitHub Desktop.
xml serialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package serialization; | |
public class AdmittedGroup { // нужен отдельный java файл AdmittedGroup.java!!! | |
public Student first = new Student("Natalie"); | |
public Student second = new Student("Olga"); | |
public Student third = new Student("Stanislav"); | |
public Student fourth = new Student("Valentine"); | |
public Student fifth = new Student("Artem"); | |
@Override | |
public String toString() { | |
return first + "\n" | |
+ second + "\n" | |
+ third + "\n" | |
+ fourth + "\n" | |
+ fifth + "\n"; | |
} | |
} | |
////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
package serialization; | |
import java.beans.XMLDecoder; | |
import java.beans.XMLEncoder; | |
import java.io.*; | |
public class Program { // нужен отдельный java файл Program.java!!! | |
public static void main(String args[]) throws IOException, ClassNotFoundException { | |
String path = "C:\\1\\1.txt"; | |
// http://en.wikipedia.org/wiki/Java_Architecture_for_XML_Binding | |
// http://docs.oracle.com/javase/8/docs/api/java/beans/XMLEncoder.html | |
AdmittedGroup group = new AdmittedGroup(); | |
System.out.println(group); | |
FileOutputStream out = new FileOutputStream(path); | |
try (XMLEncoder xmlEncoder = new XMLEncoder(out)) { | |
xmlEncoder.writeObject(group); | |
xmlEncoder.flush(); | |
} | |
FileInputStream in = new FileInputStream(path); | |
AdmittedGroup restored; | |
try (XMLDecoder xmlDecoder = new XMLDecoder(in)) { | |
restored = (AdmittedGroup) xmlDecoder.readObject(); | |
} | |
System.out.println(restored); | |
Runtime r = Runtime.getRuntime(); | |
r.exec("cmd /c " + path); | |
// r.exec("C:\\path\\to\\notepad++.exe C:\\path\\to\\file.txt"); | |
} | |
} | |
///////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
package serialization; | |
public class Student { // нужен отдельный java файл Student.java!!! | |
private String name; | |
private int age = (int) (Math.random() * 20 + 20); | |
public Student(String name) { | |
setName(name); | |
// setAge(age); | |
} | |
@Override | |
public String toString() { | |
return "Name: " + name + ", age: " + age; | |
} | |
@Override | |
public boolean equals(Object o) { | |
Student s = (Student) o; | |
return name.equals(s.name); | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
if (age >= 0 && age <= 100) { | |
this.age = age; | |
} else { | |
this.age = 18; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment