forked from Labore/PR2-L
1
0
Fork 0

added binary serialization demo

main
Gerd Marmitt 2025-04-11 09:43:31 +02:00
parent 3932019cf1
commit 7e082e31a6
5 changed files with 142 additions and 0 deletions

35
.vscode/launch.json vendored
View File

@ -4,6 +4,41 @@
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "NIOCopy",
"request": "launch",
"mainClass": "NIOCopy",
"projectName": "PR2-L_e28f9f2c"
},
{
"type": "java",
"name": "ExplicitChannelWrite",
"request": "launch",
"mainClass": "ExplicitChannelWrite",
"projectName": "PR2-L_e28f9f2c"
},
{
"type": "java",
"name": "ExplicitChannelRead",
"request": "launch",
"mainClass": "ExplicitChannelRead",
"projectName": "PR2-L_e28f9f2c"
},
{
"type": "java",
"name": "LeseDatei2",
"request": "launch",
"mainClass": "LeseDatei2",
"projectName": "PR2-L_e28f9f2c"
},
{
"type": "java",
"name": "DemoXMLSerialisierung",
"request": "launch",
"mainClass": "DemoXMLSerialisierung",
"projectName": "PR2-L_e28f9f2c"
},
{
"type": "java",
"name": "Eingabe_BufferedReader",

View File

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View File

@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

View File

@ -0,0 +1,35 @@
import java.io.*;
public class DemoBinaerSerialisierung {
public static void main(String[] args) {
Person einePerson = new Person("Mustermann", "Harald", 38);
String filename = "DemoXMLSerialisierung/src/Beispiel.bin";
// Serialisieren
try(ObjectOutputStream enc = new ObjectOutputStream(new FileOutputStream(filename)))
{
enc.writeObject(einePerson);
}
catch(IOException e)
{
e.printStackTrace();
}
// Deserialisieren
try(ObjectInputStream dec = new ObjectInputStream( new FileInputStream(filename)))
{
Person wiedereinePerson = (Person) dec.readObject();
System.out.println(wiedereinePerson.getName() + ", "
+ wiedereinePerson.getVorname() + ", Alter: "
+ wiedereinePerson.getAlter());
}
catch(IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,47 @@
import java.io.Serializable;
public class Person implements Serializable{
private String name;
private String vorname;
private int alter;
public Person() {}
public Person(String name,
String vorname, int alter)
{
this.name = name;
this.vorname = vorname;
this.alter = alter;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getVorname()
{
return vorname;
}
public void setVorname(String vorname)
{
this.vorname = vorname;
}
public int getAlter()
{
return alter;
}
public void setAlter(int alter)
{
this.alter = alter;
}
}