1
0

dodatkowa klasa do ewentualnego nadpisywania

This commit is contained in:
Piotr Baranowski 2020-04-25 10:07:37 +02:00
parent 7aeda6f11a
commit ad31122ced
3 changed files with 24 additions and 18 deletions

View File

@ -0,0 +1,21 @@
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
public class Overwrite {
public static void checkIfPeselExists(File file, String firstName, String lastName, String Pesel) {
if(file.exists()) {
try {
List<String> fileContent = new ArrayList<>(Files.readAllLines(file.toPath(), StandardCharsets.UTF_8));
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
} else {
System.out.println("Problem z odczytaniem danych z pliku.");
}
}
}

View File

@ -25,7 +25,9 @@ public class Read {
}
else {
boolean saved = false;
// sprawdzenie czy już siedzi w pliku
Overwrite.checkIfPeselExists(file, personalInfo.get(1), personalInfo.get(2), personalInfo.get(3));
if(!saved) {
Write.write(personalInfo, file);
System.out.println("Osoba dodana do listy.");

View File

@ -2,23 +2,6 @@ import java.io.*;
import java.util.List;
public class Write {
public static void checkIfPeselExists(File file, List data) {
if(file.exists()) {
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
String line;
while((line = bufferedReader.readLine()) != null) {
//
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("Problem z odczytaniem danych z pliku.");
}
}
public static void write(List data, File file) {
if(file.exists()) {
try (BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file, true))){