Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void readFromFile() {
- File fileOfRecords = new File(PATH);
- String buff;
- String surname;
- String age;
- String sex;
- String city;
- String diagnosis;
- try (BufferedReader reader = new BufferedReader(new FileReader(fileOfRecords))) {
- if (fileOfRecords.length() != 0 && !(buff = reader.readLine()).equals("")) {
- int amount = Integer.parseInt(buff);
- int i = 0;
- boolean isCorrectData = true;
- while (isCorrectData && i < amount) {
- surname = reader.readLine();
- age = reader.readLine();
- sex = reader.readLine();
- city = reader.readLine();
- diagnosis = reader.readLine();
- isCorrectData = checkData(surname, age, sex, city, diagnosis);
- if (isCorrectData) {
- Patient patient = new Patient(surname, age, sex, city, diagnosis);
- listOfPatients.add(patient);
- i++;
- }else {
- System.out.println("(Incorrect data in the file. A new file will be created automatically.)");
- listOfPatients.clear();
- }
- }
- }
- } catch (FileNotFoundException e) {
- System.out.println("(No patient information found.)");
- } catch (IOException e) {
- System.out.println("(The file is open read-only.)");
- }
- }
- static void saveToFile() {
- File fileOfRecords = new File(PATH);
- try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileOfRecords))) {
- writer.append(String.valueOf(listOfPatients.size())).append("\n");
- for (Patient Patient : listOfPatients) {
- writer.append(Patient.getSurname()).append("\n");
- writer.append(Patient.getAge()).append("\n");
- writer.append(Patient.getSex()).append("\n");
- writer.append(Patient.getCity()).append("\n");
- writer.append(Patient.getDiagnosis()).append("\n");
- }
- } catch (IOException e) {
- System.err.println("Input/output error!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement