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
| try {
final Path source = f.toPath();
final Path dest = Files.createTempFile(source.getParent(), "villeRatio", ".tmp");
final Charset cs = StandardCharsets.ISO_8859_1;
try (BufferedReader lecteurAvecBuffer = Files.newBufferedReader(source, cs);
BufferedWriter fw = Files.newBufferedWriter(dest, cs)) {
String line;
while ((line = lecteurAvecBuffer.readLine()) != null) {
String[] info = line.split(" ");
String cityRead = info[0];
String ratioRead = info[1];
String infos = "";
if (cityRead.equals(city)) {
// change ratio and write it
System.out.println("Le ratio a bien été modifié\n");
infos = cityRead + " " + ratio + "\r\n";
} else {
infos = cityRead + " " + ratioRead + "\r\n";
}
fw.write(infos);
}
} // les fichiers sont fermées automatiquement
Files.move(dest, source,
StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.ATOMIC_MOVE);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Echec de la lecture/ecriture");
} |