Dateien nach "/" hochladen
commit
c59288f627
|
@ -0,0 +1,24 @@
|
|||
package Date;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class DateFormatConverter {
|
||||
public static String convertDate(String inputDate) {
|
||||
try {
|
||||
Date date = new SimpleDateFormat("dd.MM.yyyy").parse(inputDate);
|
||||
return new SimpleDateFormat("yyyy-MM-dd").format(date);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Fehler beim Konvertieren des Datums: " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String inputDate = "25.04.2024";
|
||||
String convertedDate = convertDate(inputDate);
|
||||
if (convertedDate != null) {
|
||||
System.out.println("Ursprüngliches Datum: " + inputDate);
|
||||
System.out.println("Konvertiertes Datum: " + convertedDate);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package printF;
|
||||
|
||||
public class DoubleFormatter {
|
||||
public static void printDouble(double zahl, int nachkommastellen) {
|
||||
double multiplikator = 1;
|
||||
for (int i = 0; i < nachkommastellen; i++) {
|
||||
multiplikator *= 10;
|
||||
}
|
||||
long temp = (long) (zahl * multiplikator + 0.5);
|
||||
double gerundeterWert = (double) temp/ multiplikator;
|
||||
System.out.println(gerundeterWert);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
printDouble(1.0, 1); // Erwartete Ausgabe: 1.0
|
||||
printDouble(10.1, 1); // Erwartete Ausgabe: 10.1
|
||||
printDouble(2.01, 2); // Erwartete Ausgabe: 2.01
|
||||
printDouble(2.006, 2); // Erwartete Ausgabe: 2.01
|
||||
printDouble(2.0001, 2); // Erwartete Ausgabe: 2.00
|
||||
printDouble(2.0005, 3); // Erwartete Ausgabe: 2.001
|
||||
printDouble(123.456, 1); // Erwartete Ausgabe: 123.4
|
||||
printDouble(9876.54321, 3); // Erwartete Ausgabe: 9876.543
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package Buffer;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter("gedicht.txt"))) {
|
||||
writer.write("Mondnacht");
|
||||
writer.newLine();
|
||||
writer.write("Es war, als hätt' der Himmel");
|
||||
writer.newLine();
|
||||
writer.write("Die Erde still geküsst,");
|
||||
writer.newLine();
|
||||
writer.write("Dass sie im Blütenschimmer");
|
||||
writer.newLine();
|
||||
writer.write("Von ihm nun träumen müsst.");
|
||||
System.out.println("Das Gedicht wurde erfolgreich in die Datei geschrieben.");
|
||||
} catch (IOException e) {
|
||||
System.err.println("Fehler beim Schreiben der Datei: " + e.getMessage());
|
||||
}
|
||||
|
||||
// Reader----------------------------------------------------------
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader("gedicht.txt"))) {
|
||||
String line;
|
||||
int count = 1;
|
||||
int totalChars = 0;
|
||||
int totalWords = 0;
|
||||
int totalLines = 0;
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
System.out.printf("%3d: %s%n", count, line);
|
||||
totalChars += line.length();
|
||||
totalWords += line.split("\\s+").length;
|
||||
totalLines++;
|
||||
count++;
|
||||
}
|
||||
|
||||
System.out.println("Anzahl der Buchstaben: " + totalChars);
|
||||
System.out.println("Anzahl der Wörter: " + totalWords);
|
||||
System.out.println("Anzahl der Zeilen: " + totalLines);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Fehler beim Lesen der Datei: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package Serialisierung;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Product implements Serializable {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
private String name;
|
||||
private String description;
|
||||
private double price;
|
||||
|
||||
public Product() {}
|
||||
|
||||
public Product(String name, String description, double price) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package Serialisierung;
|
||||
import java.io.*;
|
||||
|
||||
public class SerializationExample {
|
||||
public static void main(String[] args) {
|
||||
// Erstelle ein Produkt
|
||||
Product product = new Product("Smartphone", "Ein leistungsstarkes Smartphone", 699.99);
|
||||
|
||||
// Dateiname für die Serialisierung
|
||||
String filename = "product.ser";
|
||||
|
||||
// Serialisierung
|
||||
serializeProduct(product, filename);
|
||||
|
||||
// Deserialisierung und Ausgabe
|
||||
Product deserializedProduct = deserializeProduct(filename);
|
||||
System.out.println("Deserialisiertes Produkt:");
|
||||
System.out.println("Name: " + deserializedProduct.getName());
|
||||
System.out.println("Beschreibung: " + deserializedProduct.getDescription());
|
||||
System.out.println("Preis: " + deserializedProduct.getPrice());
|
||||
}
|
||||
|
||||
// Methode zur Serialisierung eines Produkts
|
||||
private static void serializeProduct(Product product, String filename) {
|
||||
try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(filename))) {
|
||||
outputStream.writeObject(product);
|
||||
System.out.println("Produkt wurde erfolgreich serialisiert.");
|
||||
} catch (IOException e) {
|
||||
System.err.println("Fehler bei der Serialisierung des Produkts: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Methode zur Deserialisierung eines Produkts
|
||||
private static Product deserializeProduct(String filename) {
|
||||
Product product = null;
|
||||
try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(filename))) {
|
||||
product = (Product) inputStream.readObject();
|
||||
System.out.println("Produkt wurde erfolgreich deserialisiert.");
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
System.err.println("Fehler bei der Deserialisierung des Produkts: " + e.getMessage());
|
||||
}
|
||||
return product;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue