Bonjour/bonsoir,

J'ai récupéré un projet que j'ai fait il y a quelques mois (gestion d'une pharmacie) avec pour but d'y ajouter un système de sauvegarde des clients.
J'ai pour cela fait des méthodes qui exportent mes données dans des fichiers textes séparées, chaque ligne correspondant à un client.
Cependant lors de la seconde exécution de programme je ne dois stocker que les clients qui ne sont pas déjà entrés.
Avant d'aller plus loin voici mon code :
Main
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
@SuppressWarnings("unused")
public class Main {
 
	public static void main(String[] args) throws InterruptedException, FileNotFoundException, UnsupportedEncodingException {
//introduction
		File fnSave = new File("nSave.txt");
 
 
 
		System.out.println("Bienvenue");
		System.out.println("Préparation");
		Thread.sleep(1000);
		System.out.println(".");
		Thread.sleep(1000);
		System.out.println(".");
		Thread.sleep(1000);
		System.out.println(".");
		Thread.sleep(1000);
		System.out.println("Ready");
		Thread.sleep(1000);
		System.out.println("===============");
		System.out.println("Vous allez maintenant devoir les données relatives à votre stock.");
		Thread.sleep(1000);
//Input MED
		ArrayList<Medicament> medList = new ArrayList<Medicament>();
 
		Scanner scan = new Scanner(System.in);
		boolean medRestart = true;
		int iMed = 1;
 
		while(medRestart == true){
		System.out.println("Médicament numéro " + iMed+"...");
		System.out.println("Nom du médicament?");
		boolean bNomMed = true;
		String nomMed = "";
		nomMed =scan.nextLine();
		int prixMed = lireNombre(scan, "Prix du médicament?");
		medList.add(new Medicament(nomMed, prixMed));
 
		System.out.println("Autre medicament? (O/N)");
		scan.nextLine();
		char medRestartScan = scan.nextLine().toLowerCase().charAt(0);
		while(medRestartScan != 'o' && medRestartScan != 'n'){
		System.out.println("Veuillez répondre par 'O' ou 'N'.");
		medRestartScan = scan.nextLine().toLowerCase().charAt(0);
		}
		if(medRestartScan == 'n'){
			medRestart = false;
		}
		iMed++;
		}
//input CLIENTS INTRO
		ArrayList<Client> cList = new ArrayList<Client>();
		//Make sure all files exists.
		File fNames = new File("Names.txt");
		File fSurnames = new File("Surnames.txt");
		File fTowns = new File("Towns.txt");
		File fAges = new File("Ages.txt");
		if(fNames.exists() && fSurnames.exists() && fTowns.exists() && fAges.exists()){
				//LOADING SAVE.
				//ArrayLists creation.
				ArrayList<String> lNames = new ArrayList<String> ();
				ArrayList<String>  lSurnames = new ArrayList<String> ();
				ArrayList<String>  lTowns = new ArrayList<String> ();
				ArrayList<Integer> lAges = new ArrayList<Integer>();
 
				//Loading AL.
				database.DBMethods.toALString(lNames, "Names.txt");
				database.DBMethods.toALString(lSurnames, "Surnames.txt");
				database.DBMethods.toALString(lTowns, "Towns.txt");
				database.DBMethods.toALInteger(lAges, "Ages.txt");
 
				//Loading into cList.
				for(int i = 0; i< lNames.size()-2;i++){
					cList.add(new Client(lNames.get(i), lSurnames.get(i), lTowns.get(i), lAges.get(i)));
				}
 
				//correcting ints newline.
				try {
					FileWriter fwAges = new FileWriter("Ages.txt", true);
					BufferedWriter bwAges = new BufferedWriter(fwAges);
					bwAges.newLine();
					bwAges.close();
				} catch (IOException e) {
					System.out.println("Error main filewriter correctin ints.");
				}
		}
		Client.sysoutClients(cList);
 
		System.out.println("Préparation");
		Thread.sleep(1000);
		System.out.println("Prêt à recevoir les clients.");
		System.out.println("====================");
		Thread.sleep(1000);
		boolean cRestart = true;
//CLIENT INPUT DONNEES
		int ic = cList.size()+1;
		int icList = 0;
		while(cRestart ==true){
 
			System.out.println("Client numéro "+ic+"...");
			System.out.println("Nom client?");
			String nomC = scan.nextLine();
			System.out.println("Prénom client?");
			String prénomC = scan.nextLine();
			System.out.println("Ville du client?");
			String villeC = scan.nextLine();
			int ageC = lireNombre(scan, "Age du client?");
 
			cList.add(new Client(nomC, prénomC, villeC, ageC));
	//CLIENT ACHATS
			System.out.println("Preparation de la liste pour impression...");
			Thread.sleep(1000);
			System.out.println("=============");
 
			int iSizeCorrected = (medList.size())-1;
				for(int i = 0; i <= iSizeCorrected;i++){
					System.out.println(i+" : "+ medList.get(i).getNom());
				}
			boolean medBoughtStatus = true;
			int medBought = -1;
				while(medBoughtStatus == true){
					while(intervallContains(0, (medList.size()-1), medBought) == false){
					System.out.println("Med. acheté? (répondre par l'indice de la liste ci dessus).");
					boolean bMedBought = true;
					while(bMedBought == true){
					try{
					medBought = scan.nextInt();
					bMedBought = false;
					} catch(InputMismatchException e){
						System.out.println("Veuillez entrer un indice contenu dans la liste ci dessus.");
						scan.nextLine();
					}}
					}
				cList.get(icList).cMedList.add(medList.get(medBought));
				medBought = -1;
				System.out.println("Autre med. acheté? (O/N)");
				scan.nextLine();
				String medBoughtStatusBis = scan.nextLine();
				char medBoughtStatusBisChar = medBoughtStatusBis.toLowerCase().charAt(0);
					while (medBoughtStatusBisChar != 'o' && medBoughtStatusBisChar != 'n')
					{
						System.out.println("Veuillez répondre par 'O' ou 'N'.");
						medBoughtStatusBis = scan.nextLine();
						medBoughtStatusBisChar = medBoughtStatusBis.toLowerCase().charAt(0);
					}
				if(medBoughtStatusBisChar == 'n'){
					medBoughtStatus = false;
				}
				else if(medBoughtStatusBisChar == 'o'){
					medBoughtStatus = true;
				}
				}
			System.out.println("Le client numéro "+ic+" a acheté :");
			for(int iii = 0;iii< cList.get(icList).cMedList.size() ;iii++){
				System.out.println(cList.get(icList).cMedList.get(iii).getNom()+" a "+cList.get(icList).cMedList.get(iii).getPrix()+" euro(s).");
 
			}
//NEXT CLIENT (?)
		ic++;
		icList++;
		System.out.println("Autre client? (O/N)");
		String charC = scan.nextLine().toLowerCase();
		boolean bCharC = true;
		while(bCharC == true){
		if(charC.charAt(0) != 'n' && charC.charAt(0) != 'o'){
			System.out.println("Veuillez répondre par 'O' ou 'N'.");
			charC = scan.nextLine().toLowerCase();
		}
		else {
			bCharC = false;
		}
		}
		if(charC.charAt(0)=='n')
			cRestart = false;
 
		}
 
		//Final part
		System.out.println("Fin de l'encodage client.");
		System.out.println("Sous quel nom voulez-vous sortir l'historique?");
		String nFile = scan.nextLine();
		System.out.println("Impression de l'historique dans 3 secondes.");
		Thread.sleep(3000);
		int iLast =0;
		for(iLast = 0; iLast < cList.size(); iLast++){
			System.out.println("Le client "+cList.get(iLast).getNom()+" "+cList.get(iLast).getPrenom()+" a acheté :");
			cList.get(iLast).PrintCMedList();
		}
 
 
		PrintWriter writer = new PrintWriter(nFile+".txt", "UTF-8");
		for(int iWriteCPos = 0; iWriteCPos < cList.size();iWriteCPos++){
			for(int iWriteMedPos = 0; iWriteMedPos < cList.get(iWriteCPos).cMedList.size();iWriteMedPos++){
				writer.println(cList.get(iWriteCPos).toStringCMed(iWriteMedPos));
			}
		}
		System.out.println("Liste des ventes sauvegardée sous '"+nFile+".txt'.");
 
		System.out.println("Voulez-vous aussi sauvegarder la liste des clients?(o/n)");
		String saveC = scan.nextLine().toLowerCase();
		while(saveC.charAt(0) != 'o' && saveC.charAt(0) != 'n'){
			System.out.println("Veuillez répondre par 'o' ou 'n'.");
			saveC = scan.nextLine().toLowerCase();
		}
		if(saveC.charAt(0) == 'o'){
			writer.println("================");
			writer.println("Liste des clients : ");
 
		for(int cWriter = 0;cWriter < cList.size();cWriter++){
			writer.println(cList.get(cWriter).toString());	
		}
		//number of entries save.
 
		ArrayList<Integer> nSave = new ArrayList<Integer>();
		nSave.add(cList.size());
 
		try {
			fnSave.delete();
			fnSave.createNewFile();
		} catch (IOException e) {
			System.out.println("nSave Error.");
		}
		database.DBMethods.toDBInteger(nSave, "nSave.txt");
		FileReader frnSave = new FileReader(fnSave);
		BufferedReader brnSave = new BufferedReader(frnSave);
		int nSaveInt=0;
		//TODO RESOUDRE LE PROBLEME ICI. (N'ECRIS PAS AVEC LA METHODE IMPLIQUANT nSave.)
		try {
			nSaveInt = Integer.parseInt(brnSave.readLine().trim());
			brnSave.close();
 
		} catch (NumberFormatException e) {
			System.out.println("nSaveInt Error.");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("nSaveIntError");
			e.printStackTrace();
		}
		//name arraylist
		ArrayList<String> cNames = new ArrayList<String>();
		for(int i=0; i< cList.size();i++){
			cNames.add(cList.get(i).getNom());
		}
		//surname arraylist
		ArrayList<String> cSurnames = new ArrayList<String>();
		for(int i=0; i< cList.size(); i++){
			cSurnames.add(cList.get(i).getPrenom());
		}
		//town Arraylist
		ArrayList<String> cTown = new ArrayList<String>();
		for(int i=0; i< cList.size(); i++){
			cTown.add(cList.get(i).getVille());
		}
		//Age ArrayList
		ArrayList<Integer> cAge = new ArrayList<Integer>();
		for(int i=0; i< cList.size(); i++){
			cAge.add(cList.get(i).getAge());
		}
 
		//Create save files
		database.DBMethods.toDBString(cNames, "Names.txt", nSaveInt);
		database.DBMethods.toDBString(cNames, "Names.txt");
		database.DBMethods.toDBString(cSurnames, "Surnames.txt", nSaveInt);
		database.DBMethods.toDBString(cTown, "Towns.txt", nSaveInt);
		database.DBMethods.toDBInteger(cAge, "Ages.txt", nSaveInt);
 
		System.out.println("Liste des clients sauvegardée.");
		}
 
		writer.close();
		scan.close();
		System.out.println("Fin de la session.");
		System.out.println("END.");
	}
	public static boolean intervallContains(int low, int high, int n) {
	    return n >= low && n <= high;
	}
 
	public static int lireNombre(Scanner scan, String phrase){ //Demande un nombre, le lis et recommence jusqu'à ce que nomre soit un entier valable.
		System.out.println(phrase);
		int output = 0;
		boolean b = true;
		while(b == true){
			try{
				output = scan.nextInt();
				b = false;
			} catch(InputMismatchException e){
				System.out.println("Veuillez entrer un nombre entier.");
				scan.nextLine();
			}
		}
		return output;
	}
}
Client
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 
import java.util.ArrayList;
 
public class Client {
private String Nom, prenom, ville;
private int age;
ArrayList<Medicament> cMedList = new ArrayList<Medicament>();
 
public Client (){
	this.Nom = "?";
	this.prenom = "?";
	this.ville = "?";
	this.age = -1;
}
 
public Client(String nom, String prenom, String ville, int age) {
	super();
	Nom = nom;
	this.prenom = prenom;
	this.ville = ville;
	this.age = age;
}
 
public String getNom() {
	return Nom;
}
 
public void setNom(String nom) {
	Nom = nom;
}
 
public String getPrenom() {
	return prenom;
}
 
public void setPrenom(String prenom) {
	this.prenom = prenom;
}
 
public String getVille() {
	return ville;
}
 
public void setVille(String ville) {
	this.ville = ville;
}
 
public int getAge() {
	return age;
}
 
public void setAge(int age) {
	this.age = age;
}
//arraylist gestion
public void cAddMed (Medicament med){
	cMedList.add(med);
}
 
@Override
public String toString() {
	return "Client [Nom=" + Nom + ", prenom=" + prenom + ", ville=" + ville + ", age=" + age+"]";
}
public void PrintCMedList() {
	for(int i=0; i<this.cMedList.size();i++){
		System.out.println("-"+this.cMedList.get(i).getNom()+" à "+this.cMedList.get(i).getPrix()+" euros.");
	}
}
public String toStringCMed(int medPos){
	return "Le client "+this.getNom()+" "+this.getPrenom()+" a acheté un " + this.cMedList.get(medPos).getNom() + " à "+ this.cMedList.get(medPos).getPrix()+"euros.";
}
 
public static void sysoutClients(ArrayList<Client> cList){
	for(int i=0; i< cList.size(); i++){
		System.out.println(cList.get(i));
	}
}
 
 
 
}
database
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
 
package database;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
 
public class DBMethods {
//prints arraylist to a file.
	//New line for every data.
	public static void toDBString(ArrayList<String> al, String fName){
		File file = new File(fName);
		try {
			FileWriter fw = new FileWriter(file, true);
			BufferedWriter bw = new BufferedWriter(fw);
			for(int i=0; i< al.size(); i++){
				bw.write(al.get(i));
				bw.newLine();
			}
			bw.close();
			fw.close();
		} catch (FileNotFoundException e) {
			System.out.println("Error, toDB string.");
		} catch (IOException e) {
			System.out.println("Error, toDB string.");		}
 
	}
 
	//idem + number, j = number to start printing
	public static void toDBString(ArrayList<String> al, String fName, int j){
		File file = new File(fName);
		try {
			FileWriter fw = new FileWriter(file, true);
			BufferedWriter bw = new BufferedWriter(fw);
			for(int i=j; i< al.size(); i++){
				bw.write(al.get(i));
				bw.newLine();
			}
			bw.close();
			fw.close();
		} catch (FileNotFoundException e) {
			System.out.println("Error, toDB string.");
		} catch (IOException e) {
			System.out.println("Error, toDB string.");		}
 
	}
//Idem, int.
	public static void toDBInteger(ArrayList<Integer> al, String fName){
		File file = new File(fName);
		try {
			FileWriter fw = new FileWriter(file, true);
			BufferedWriter bw = new BufferedWriter(fw);
			for(int i=0; i< al.size(); i++){
				bw.write(al.get(i).toString());
				if(i!=al.size()-1)
				bw.newLine();
			}
			bw.close();
			fw.close();
		} catch (FileNotFoundException e) {
			System.out.println("Error, toDB string.");
		} catch (IOException e) {
			System.out.println("Error, toDB string.");		}
 
	}
 
	public static void toDBInteger(ArrayList<Integer> al, String fName, int j){
		File file = new File(fName);
		try {
			FileWriter fw = new FileWriter(file, true);
			BufferedWriter bw = new BufferedWriter(fw);
			for(int i=j; i< al.size(); i++){
				bw.write(al.get(i).toString());
				if(i!=al.size()-1)
				bw.newLine();
			}
			bw.close();
			fw.close();
		} catch (FileNotFoundException e) {
			System.out.println("Error, toDB string.");
		} catch (IOException e) {
			System.out.println("Error, toDB string.");		}
 
	}
//From file to ArrayList.
	public static void toALString(ArrayList<String> al, String fName){
		File file = new File(fName);
		try {
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String s = "a";
			while(s != null){
				s=br.readLine();
				al.add(s);
			}
			br.close();
			fr.close();
		} catch (IOException e) {
			System.out.println("Error, toALString.");
		}
 
	}
 
	public static void toALInteger(ArrayList<Integer> al, String fName){
		File file = new File(fName);
		try {
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String s = "a";
			while(s != null){
				s=br.readLine();
				if(s!= null){
				Integer in = new Integer(s.trim());
				al.add(in);
			}}
			br.close();
			fr.close();
		} catch (IOException e) {
			System.out.println("Error, toALInteger.");
		}
 
	}
}
Donc voici mon problème :
La méthode toDBString(ArrayList<String> al, String fName), cependant lors de la prochaine exécution je ne veux envoyer que les nouveaux clients donc je rajoute un int dans la même méthode comme ceci et je commence à partir de ce nombre, c'est la méthode toDBString(ArrayList<String> al, String fName, int j) mais celle-ci n'écris RIEN dans les fichiers textes ! (Qui sont cependant biens créés :/ ).

L'écriture dans les fichiers commence à la ligne 225 pour que vous ne deviez pas trop chercher ^^

Merci d'avance de votre aide et bon Dimanche