Skip to content

Instantly share code, notes, and snippets.

@shdxw
Created January 14, 2021 03:55
Show Gist options
  • Select an option

  • Save shdxw/963583d454286e7de40463e51a75bc81 to your computer and use it in GitHub Desktop.

Select an option

Save shdxw/963583d454286e7de40463e51a75bc81 to your computer and use it in GitHub Desktop.
package ru.java.serialization;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Human implements Serializable {
private String name;
private String secondName;
private Calendar birthDate;
private String house;
private int socialNumber;
public Human(String name, String secondName, Calendar birthDate, String house, int socialNumber) {
this.name = name;
this.secondName = secondName;
this.birthDate = birthDate;
this.house = house;
this.socialNumber = socialNumber;
}
@Override
public String toString() {
return "Human{" +
"name='" + name + '\'' +
", secondName='" + secondName + '\'' +
", birthDate=" + birthDate.getTime() +
", house='" + house + '\'' +
", socialNumber=" + socialNumber +
'}';
}
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar(2017, Calendar.JANUARY, 25);
Date date = calendar.getTime();
}
}
package ru.java.serialization;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Roulette {
List<Integer> mas = new ArrayList<>();
List<Integer> exp = List.of(0,1,2,3,4,5,6,7,8);
int mod = 0;
void trying() {
Random ran = new Random();
int random = ran.nextInt(9);
mod++;
if (!mas.contains(random)) {
mas.add(random);
}
}
int show() {
while (!mas.equals(exp)) {
trying();
System.out.println(mas.toString());
}
return mod;
}
public static void main(String[] args) {
Roulette r = new Roulette();
System.out.println(r.show());
}
}
package ru.java.serialization;
import java.io.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Worker {
public Worker() {
}
public void write(Human human) throws IOException {
FileOutputStream outputStream = new FileOutputStream("C:\\projects\\labs\\src\\main\\java\\ru\\java\\serialization\\plan.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(human);
objectOutputStream.close();
}
public Human read() {
try(ObjectInputStream in = new ObjectInputStream(new FileInputStream("C:\\projects\\labs\\src\\main\\java\\ru\\java\\serialization\\plan.txt")))
{
return (Human)in.readObject();
}
catch(Exception ex){
return null;
}
}
public void write2(ArrayList<Human> people) {
String filename = "C:\\projects\\labs\\src\\main\\java\\ru\\java\\serialization\\plan2.txt";
// создадим список объектов, которые будем записывать
try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename)))
{
oos.writeObject(people);
System.out.println("File has been written");
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
public ArrayList<Human> read2() {
// десериализация в новый список
ArrayList<Human> newPeople= new ArrayList<Human>();
String filename = "C:\\projects\\labs\\src\\main\\java\\ru\\java\\serialization\\plan2.txt";
try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename)))
{
return ((ArrayList<Human>)ois.readObject());
}
catch(Exception ex){
return null;
}
}
public static void main(String[] args) throws IOException {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment