nadpisywanie danych w pliku
This commit is contained in:
parent
ad31122ced
commit
a2a20ce356
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,5 @@
|
|||||||
.idea/
|
.idea/
|
||||||
|
|
||||||
target/
|
target/
|
||||||
|
|
||||||
|
[a-zA-Z0-9]* lista.txt
|
@ -3,19 +3,52 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class Overwrite {
|
public class Overwrite {
|
||||||
public static void checkIfPeselExists(File file, String firstName, String lastName, String Pesel) {
|
public static boolean checkIfPeselExists(File file, List personalData) {
|
||||||
if(file.exists()) {
|
if(file.exists()) {
|
||||||
|
List<String> fileContent = new ArrayList<>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
List<String> fileContent = new ArrayList<>(Files.readAllLines(file.toPath(), StandardCharsets.UTF_8));
|
fileContent = new ArrayList<>(Files.readAllLines(file.toPath(), StandardCharsets.UTF_8));
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
System.out.println(ex.getMessage());
|
System.out.println(ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Pattern patternFirstName = Pattern.compile(personalData.get(1).toString());
|
||||||
|
Pattern patternLastName = Pattern.compile(personalData.get(2).toString());
|
||||||
|
Pattern patternPesel = Pattern.compile(personalData.get(3).toString());
|
||||||
|
|
||||||
|
boolean peselInFile = false;
|
||||||
|
|
||||||
|
for(int i = 0; i < fileContent.size(); i++) {
|
||||||
|
if(patternPesel.matcher(fileContent.get(i)).find() && !patternFirstName.matcher(fileContent.get(i)).find() &&
|
||||||
|
!patternLastName.matcher(fileContent.get(i)).find()) {
|
||||||
|
String dataToOverwrite = personalData.get(0).toString() + " " + personalData.get(1) + " "
|
||||||
|
+ personalData.get(2) + " " + personalData.get(3);
|
||||||
|
fileContent.set(i, dataToOverwrite);
|
||||||
|
peselInFile = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(peselInFile) {
|
||||||
|
try {
|
||||||
|
Files.write(file.toPath(), fileContent, StandardCharsets.UTF_8);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
System.out.println(ex.getMessage());
|
||||||
|
}
|
||||||
|
System.out.println("Osoba o podanym PESEL-u została już wcześniej wprowadzona. Dane zostały nadpisane.");
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Problem z odczytaniem danych z pliku.");
|
System.out.println("Problem z odczytaniem danych z pliku.");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,12 +24,10 @@ public class Read {
|
|||||||
System.out.println("Wprowadzony PESEL jest niepoprawny.");
|
System.out.println("Wprowadzony PESEL jest niepoprawny.");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
boolean saved = false;
|
boolean saved = Overwrite.checkIfPeselExists(file, personalInfo);
|
||||||
|
|
||||||
Overwrite.checkIfPeselExists(file, personalInfo.get(1), personalInfo.get(2), personalInfo.get(3));
|
|
||||||
|
|
||||||
if(!saved) {
|
if(!saved) {
|
||||||
Write.write(personalInfo, file);
|
Write.write(file, personalInfo);
|
||||||
System.out.println("Osoba dodana do listy.");
|
System.out.println("Osoba dodana do listy.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ import java.io.*;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Write {
|
public class Write {
|
||||||
public static void write(List data, File file) {
|
public static void write(File file, List data) {
|
||||||
if(file.exists()) {
|
if(file.exists()) {
|
||||||
try (BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file, true))){
|
try (BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file, true))){
|
||||||
fileWriter.write(data.get(0) + " " + data.get(1) + " " + data.get(2) + " " + data.get(3) + "\n");
|
fileWriter.write(data.get(0) + " " + data.get(1) + " " + data.get(2) + " " + data.get(3) + "\n");
|
||||||
@ -10,7 +10,6 @@ public class Write {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
System.out.println("Zapis danych do pliku zakończył się powodzeniem.");
|
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Problem z zapisem do pliku.");
|
System.out.println("Problem z zapisem do pliku.");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user