@ -0,0 +1,29 @@ | |||||
### IntelliJ IDEA ### | |||||
out/ | |||||
!**/src/main/**/out/ | |||||
!**/src/test/**/out/ | |||||
### Eclipse ### | |||||
.apt_generated | |||||
.classpath | |||||
.factorypath | |||||
.project | |||||
.settings | |||||
.springBeans | |||||
.sts4-cache | |||||
bin/ | |||||
!**/src/main/**/bin/ | |||||
!**/src/test/**/bin/ | |||||
### NetBeans ### | |||||
/nbproject/private/ | |||||
/nbbuild/ | |||||
/dist/ | |||||
/nbdist/ | |||||
/.nb-gradle/ | |||||
### VS Code ### | |||||
.vscode/ | |||||
### Mac OS ### | |||||
.DS_Store |
@ -0,0 +1,8 @@ | |||||
# Default ignored files | |||||
/shelf/ | |||||
/workspace.xml | |||||
# Editor-based HTTP Client requests | |||||
/httpRequests/ | |||||
# Datasource local storage ignored files | |||||
/dataSources/ | |||||
/dataSources.local.xml |
@ -0,0 +1,6 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project version="4"> | |||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="17" project-jdk-type="JavaSDK"> | |||||
<output url="file://$PROJECT_DIR$/out" /> | |||||
</component> | |||||
</project> |
@ -0,0 +1,8 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project version="4"> | |||||
<component name="ProjectModuleManager"> | |||||
<modules> | |||||
<module fileurl="file://$PROJECT_DIR$/com.createXml.iml" filepath="$PROJECT_DIR$/com.createXml.iml" /> | |||||
</modules> | |||||
</component> | |||||
</project> |
@ -0,0 +1,6 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project version="4"> | |||||
<component name="VcsDirectoryMappings"> | |||||
<mapping directory="$PROJECT_DIR$" vcs="Git" /> | |||||
</component> | |||||
</project> |
@ -0,0 +1,11 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<module type="JAVA_MODULE" version="4"> | |||||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | |||||
<exclude-output /> | |||||
<content url="file://$MODULE_DIR$"> | |||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | |||||
</content> | |||||
<orderEntry type="inheritedJdk" /> | |||||
<orderEntry type="sourceFolder" forTests="false" /> | |||||
</component> | |||||
</module> |
@ -0,0 +1,32 @@ | |||||
import controller.ObjReader; | |||||
import controller.ObjWriter; | |||||
import model.People; | |||||
import model.Person; | |||||
import java.io.File; | |||||
public class Main { | |||||
public static void main(String[] args) { | |||||
File file = new File("person.bin"); | |||||
//Create list | |||||
People data = new People(); | |||||
//Init list | |||||
data.add(new Person("Manolo", 32)); | |||||
data.add(new Person("Jorge", 2)); | |||||
data.add(new Person("Pepe", 89)); | |||||
//Check writing | |||||
boolean isWritten = ObjWriter.write(data,file); | |||||
//Test serialization | |||||
People p; | |||||
if (isWritten) { | |||||
p = (People) ObjReader.read(file); | |||||
System.out.println(p.toString()); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,20 @@ | |||||
package controller; | |||||
import java.io.File; | |||||
import java.io.FileInputStream; | |||||
import java.io.IOException; | |||||
import java.io.ObjectInputStream; | |||||
public class ObjReader { | |||||
public static Object read(File file) { | |||||
Object result = null; | |||||
try (FileInputStream fis = new FileInputStream(file); | |||||
ObjectInputStream ois = new ObjectInputStream(fis) | |||||
){ | |||||
result = ois.readObject(); | |||||
} catch (IOException | ClassNotFoundException e) { | |||||
throw new RuntimeException(e); | |||||
} | |||||
return result; | |||||
} | |||||
} |
@ -0,0 +1,20 @@ | |||||
package controller; | |||||
import java.io.File; | |||||
import java.io.FileOutputStream; | |||||
import java.io.IOException; | |||||
import java.io.ObjectOutputStream; | |||||
public class ObjWriter { | |||||
public static boolean write(Object obj, File out) { | |||||
try (FileOutputStream fos = new FileOutputStream(out); | |||||
ObjectOutputStream oos = new ObjectOutputStream(fos) | |||||
){ | |||||
oos.writeObject(obj); | |||||
oos.flush(); | |||||
return true; | |||||
} catch (IOException e) { | |||||
throw new RuntimeException(e); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,34 @@ | |||||
package model; | |||||
import java.io.Serial; | |||||
import java.io.Serializable; | |||||
import java.util.ArrayList; | |||||
public class People implements Serializable { | |||||
@Serial | |||||
private static final long serialVersionUID = 1L; | |||||
private ArrayList<Person> data ; | |||||
public People() { | |||||
data = new ArrayList<>(); | |||||
} | |||||
public boolean add(Person p){ | |||||
data.add(p); | |||||
return true; | |||||
} | |||||
public boolean remove(Person p){ | |||||
data.remove(p); | |||||
return true; | |||||
} | |||||
public ArrayList<Person> getData() { | |||||
return data; | |||||
} | |||||
@Override | |||||
public String toString() { | |||||
return data.toString(); | |||||
} | |||||
} |
@ -0,0 +1,37 @@ | |||||
package model; | |||||
import java.io.Serializable; | |||||
public class Person implements Serializable { | |||||
private static int CONT = 0; | |||||
private int id ; | |||||
private String name; | |||||
private int age; | |||||
public Person(String name, int age) { | |||||
id = ++CONT; | |||||
this.name = name; | |||||
this.age = age; | |||||
} | |||||
public String getName() { | |||||
return name; | |||||
} | |||||
public void setName(String name) { | |||||
this.name = name; | |||||
} | |||||
public int getAge() { | |||||
return age; | |||||
} | |||||
public void setAge(int age) { | |||||
this.age = age; | |||||
} | |||||
@Override | |||||
public String toString() { | |||||
return "\nID = " + id + ", NAME = " + name + ", AGE = " + age; | |||||
} | |||||
} |