LogTest.java

package de.hs_mannheim.informatik.mvn;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LogTest {
    private static final Logger logger = LogManager.getLogger(LogTest.class);

    public static void newRecord(String path, String username, String time) throws FileNotFoundException {
        String timePart = time.substring("Zeit: ".length()).trim();
        String[] parts = path.split("/");
        String filename = parts[parts.length - 1];
        int dotIndex = filename.lastIndexOf(".");
        String result = filename.substring(0, dotIndex);
        String ordner = "C:/Users/Berat/Desktop/hitoriNew/src/main/java/de/hs_mannheim/informatik/mvn/Hitori_Highscores/";
        String filetype = ".txt";
        String filepath = ordner + result + filetype;
        System.out.println("A: " + filepath);
        Scanner sc = new Scanner(filepath);
        while (sc.hasNextLine()) {
            sc.nextLine();
        }
        sc.close();

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filepath, true))) {
            String eintrag = timePart + " " + username;
            logger.info("Neuer Eintrag: {}", eintrag);

            writer.write(eintrag);
            writer.newLine();
        } catch (IOException e) {
            logger.error("Fehler beim Schreiben in die Datei: " + filepath, e);
        }
    }
}