Browse Source

Project setup

main
Jesus 2 years ago
commit
b5e5b6dab1
12 changed files with 211 additions and 0 deletions
  1. +29
    -0
      .gitignore
  2. +8
    -0
      .idea/.gitignore
  3. +6
    -0
      .idea/misc.xml
  4. +8
    -0
      .idea/modules.xml
  5. +6
    -0
      .idea/vcs.xml
  6. +11
    -0
      com.createXml.iml
  7. BIN
      person.bin
  8. +32
    -0
      src/Main.java
  9. +20
    -0
      src/controller/ObjReader.java
  10. +20
    -0
      src/controller/ObjWriter.java
  11. +34
    -0
      src/model/People.java
  12. +37
    -0
      src/model/Person.java

+ 29
- 0
.gitignore View File

@ -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

+ 8
- 0
.idea/.gitignore View File

@ -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

+ 6
- 0
.idea/misc.xml View File

@ -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>

+ 8
- 0
.idea/modules.xml View File

@ -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>

+ 6
- 0
.idea/vcs.xml View File

@ -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>

+ 11
- 0
com.createXml.iml View File

@ -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>

BIN
person.bin View File


+ 32
- 0
src/Main.java View File

@ -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());
}
}
}

+ 20
- 0
src/controller/ObjReader.java View File

@ -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;
}
}

+ 20
- 0
src/controller/ObjWriter.java View File

@ -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);
}
}
}

+ 34
- 0
src/model/People.java View File

@ -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();
}
}

+ 37
- 0
src/model/Person.java View File

@ -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;
}
}

Loading…
Cancel
Save