ConfigDatei. Persistenz und Datenmodell

master
BretschneiderMarvin 2023-04-05 12:27:49 +02:00
parent 98b2e9b410
commit a085c73e7e
4 changed files with 189 additions and 26 deletions

View File

@ -1,4 +1,5 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
@ -8,29 +9,32 @@ import java.util.Scanner;
public class ConfigDatei {
private String firmenname = "Firma1";
private String firmenname;
private String Firmenadresse;
private String Bankverbindung;
private static String configFile = "config.cfg";
public static void main(String[] args) {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
File tmpFile = new File("C:\\Users\\Tim\\OneDrive\\Desktop\\b.txt");
char quote = '"';
File tmpFile = new File(configFile);
boolean exists = tmpFile.exists();
if (exists = false) {
if (!tmpFile.exists()) {
try {
FileWriter fw = new FileWriter(configFile);
System.out.println("Bitte geben sie Ihren Firmennamen ein: ");
tmpFile.createNewFile();
BufferedWriter bw = new BufferedWriter(new FileWriter(tmpFile));
System.out.println("Bitte geben sie Ihren Firmennamen ein: ");//Benutzereingaben
String name = sc.nextLine();
System.out.println("Bitte geben sie Ihre Firmenadresse ein: ");
String adresse = sc.nextLine();
System.out.println("Bitte geben sie Ihre Bankverbindung ein: ");
String bankverbindung = sc.nextLine();
char quote = '"';
fw.write("Firmenname: " + quote + name);
fw.write("Firmenadresse: " +quote +adresse);
fw.write("Bankverbindung: " +);
System.out.println("Bitte geben Sie ihr Land ein: ");
String land = sc.nextLine();
bw.write("Firmenname= " + quote + name + quote + "\n");//in datei schreiben schreiben
bw.write("Firmenadresse= " + quote + adresse + quote + "\n");
bw.write("Bankverbindung= " + quote + bankverbindung + quote + "\n");
bw.write("Land= " + quote + land + quote + "\n");
bw.close();//bw schließen
} catch (IOException e) {
e.printStackTrace();
@ -39,8 +43,19 @@ public class ConfigDatei {
}
else {
BufferedReader br= new BufferedReader(new FileReader(configFile));
br.read();
}}
try {
BufferedReader br = new BufferedReader(new FileReader(tmpFile));
String zeile;
while ((zeile = br.readLine()) != null) {
String werte[] = zeile.split("=");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}

View File

@ -0,0 +1,90 @@
import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ConfigDateiXML {
private String firmenname;
private String firmenadresse;
private String bankverbindung;
private String land;
public ConfigDateiXML() {
}
public ConfigDateiXML(String firmenname, String firmenadresse, String bankverbindung, String land) {
this.firmenname = firmenname;
this.firmenadresse = firmenadresse;
this.bankverbindung = bankverbindung;
this.land = land;
}
public String getFirmenname() {
return firmenname;
}
public void setFirmenname(String firmenname) {
this.firmenname = firmenname;
}
public String getFirmenadresse() {
return firmenadresse;
}
public void setFirmenadresse(String firmenadresse) {
this.firmenadresse = firmenadresse;
}
public String getBankverbindung() {
return bankverbindung;
}
public void setBankverbindung(String bankverbindung) {
this.bankverbindung = bankverbindung;
}
public String getLand() {
return land;
}
public void setLand(String land) {
this.land = land;
}
public static void main(String[] args) {
ConfigDateiXML config = new ConfigDateiXML("Firma X", "Musterstraße 1", "DE123456789", "Deutschland");
try (XMLEncoder encoder = new XMLEncoder(new FileOutputStream("config.xml"))) {
encoder.writeObject(config);
} catch (IOException e) {
e.printStackTrace();
}
// XML-Decoder
try (XMLDecoder decoder = new XMLDecoder(new FileInputStream("config.xml"))) {
ConfigDateiXML decodedConfig = (ConfigDateiXML) decoder.readObject();
System.out.println(decodedConfig.getFirmenname());
System.out.println(decodedConfig.getFirmenadresse());
System.out.println(decodedConfig.getBankverbindung());
System.out.println(decodedConfig.getLand());
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -2,17 +2,33 @@
public class Datenmodell {
private String firmenname;
private String Firmenadresse;
private String Bankverbindung;
private String firmenadresse;
private String bankverbindung;
private int steuernummer;
public void setFirmenname(String firmenname) {
this.firmenname = firmenname;
}
public void setFirmenadresse(String firmenadresse) {
this.firmenadresse = firmenadresse;
}
public void setBankverbindung(String bankverbindung) {
this.bankverbindung = bankverbindung;
}
public void setSteuernummer(int steuernummer) {
this.steuernummer = steuernummer;
}
public String getFirmenname() {
return firmenname;
}
public int getSteuernummer() {
return steuernummer;
}
public String getFirmenadresse() {
return Firmenadresse;
return firmenadresse;
}
public String getBankverbindung() {
return Bankverbindung;
return bankverbindung;
}
}

View File

@ -1,9 +1,51 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Persistenz {
public static void main(String[] args) {
// TODO Auto-generated method stub
public static final String FILEPATH = "C:\\Users\\Tim\\OneDrive\\Desktop\\c.txt";
public void speichern(Datenmodell daten) throws IOException {
File file = new File(FILEPATH);
if (!file.exists()) {
file.createNewFile();
}
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
char quote = '"';
bw.write("Firmenname: " + quote + daten.getFirmenname() + quote + "\n");
bw.write("Firmenadresse: " + quote + daten.getFirmenadresse() + quote + "\n");
bw.write("Bankverbindung: " + quote + daten.getBankverbindung() + quote + "\n");
}
public Datenmodell laden() throws IOException {
Datenmodell daten = new Datenmodell();
BufferedReader br = new BufferedReader(new FileReader(FILEPATH));
String zeile;
while ((zeile = br.readLine()) != null) {
String werte[] = zeile.split("=");
String name = werte[0].trim();
String value = werte[1].trim().replace("/", "");
switch (name) {
case "Firmenname":
daten.setBankverbindung(value);
break;
case "Firmenadresse":
daten.setFirmenadresse(value);
break;
case "Bankverbindung":
daten.setBankverbindung(value);
break;
}
}
br.close();
return daten;
}
}