Input/Output Konstruktor ergänzen, der zur Datei mit einem Objekt passt

[CODE lang="java" title="In Zeile 26 Steht die Aufgabe" highlight="26"]import java.io.*;

public class DataStreamBsp {
private String s;
private int i;
private byte b;
private double d;
private boolean t;
//******************************************
public DataStreamBsp(String s, int i, byte b, double d, boolean t){
this.s = s;
this.i = i;
this.b = b;
this.d = d;
this.t = t;
}

public DataStreamBsp(){
}

//passt zu den Array-Datein
public DataStreamBsp (DataInputStream ds) throws IOException{
laden(ds);
}

//HUE: Konstruktor ergänzen, der zur Datei mit einem Objekt passt


//***************************************
public String toString(){
return "s = " +s
+", i= "+i
+", b= "+b
+", d= "+d
+", t= "+t;
}
//**************************************
public boolean speichern(String fileName){
try{
DataOutputStream ds = new DataOutputStream(new FileOutputStream(fileName));

boolean rc = this.speichern(ds);

ds.close();
return rc;
}catch (IOException e){
System.out.println("Speichern ging schief: \n"+e.getMessage());
}
return false;
}

//*******************************
public static boolean speichern(String fileName, DataStreamBsp[] daten){
try{
boolean rc = true;
DataOutputStream ds = new DataOutputStream (new FileOutputStream(fileName));

ds.writeInt(daten.length);
for (int nr = 0; nr < daten.length; nr++){
rc = rc && daten[nr].speichern(ds);
}

ds.close();
return rc;
}catch (IOException e){
System.out.println("Speichern ging schief: \n"+e.getMessage());
}
return false;
}

//***************************************

public boolean speichern(DataOutputStream ds) {
try {
ds.writeUTF(s);
ds.writeInt(i);
ds.writeByte(b);
ds.writeDouble(d);
ds.writeBoolean(t);
//........
//es gibt auch writeFloat, writeShort, writeLong...

return true;
} catch (IOException e) {
System.out.println("Speichern ging schief: \n" + e.getMessage());
}
return false;
}

//*****************************************

public boolean laden(String fileName){

try{
DataInputStream ds = new DataInputStream(new FileInputStream(fileName));

boolean rc = laden(ds);

ds.close();
return rc;
}catch (IOException e){
System.out.println("Laden ging schief: \n" + e.getMessage());
}
return false;
}

public boolean laden(DataInputStream ds){
try {

s = ds.readUTF(); //Alternativen: writeChars, writeBytes
i = ds.readInt();
b = ds.readByte();
d = ds.readDouble();
t = ds.readBoolean();
//es gibt auch writeFloat, writeShort, writeLong....

return true;
}catch (IOException e) {
System.out.println("Ladne ging schief: \n" + e.getMessage());
}
return false;
}

public static DataStreamBsp[] ladenArray(String fileName){
try (DataInputStream ds = new DataInputStream(new FileInputStream(fileName))😉{

int anzahl = ds.readInt();
DataStreamBsp[] daten = new DataStreamBsp[anzahl];

for (int nr = 0; nr < daten.length; nr ++) {
daten[nr] = new DataStreamBsp();
if (!daten[nr].laden(ds))
return null;
}

return daten;
}catch (IOException e) {
System.out.println("Laden ging schief: \n" + e.getMessage());
}
return null;
}





//********************
//HÜ!!
//Ue: (1) schreibe eine Methode, die ein Array von solchen Objekte speichert
// (2) schreibe eine Methode, die eine solche Datei hereinliest ---> liefert also ein Array...
// (3) test!
// (4) schreibe einen Konstrukor, der einen Filenamen übernimmt und das Objekt aus dieser Datei initialiesiert


//zum Testen
public static void main(String[] args) {
DataStreamBsp ds1 = new DataStreamBsp("Hallo",
12,
(byte) 1,
1.7321,
true);

System.out.println(ds1 + "\n ---> das war ds1");
ds1.speichern("Bin1.BIN3i");

DataStreamBsp ds2 = new DataStreamBsp("Good Morning",
7,
(byte) 3,
3.14159165,
false);
System.out.println(ds2 + "\n ---> das war ds2");
ds2.speichern("Bin2.BIN3i");

//absichtlich 1 ->2, 2 ->1, damit man auch etwas sieht....

ds1.laden("Bin2.BIN3i");
ds2.laden("Bin1.BIN3i");

System.out.println(ds1 + "\n ---> das war ds1");
System.out.println(ds2 + "\n ---> das war ds2");

DataStreamBsp[] data = {ds1,
ds2
};

speichern("ds1_2.ARRAY", data);

DataStreamBsp[] dataGeladen = ladenArray("ds1_2.ARRAY");

System.out.println("gespeichert und geladen: ");
for (int nr = 0; nr < dataGeladen.length; nr++) {
System.out.println("Eintrag " + nr + " \n*****");
System.out.println(dataGeladen[nr]);
}
}
}
[/CODE]
 

Neue Themen


Zurück
Oben