Bonjour internautes!
J'ai besoin d'un aiguillage sur l'écriture d'un programme qui aurait pour fonction de copier un fichier texte, de lire la copie, et remplacer certaines chaines de caractères dans ce fichier selon ce que l'utilisateur entre au clavier. J'ai fais les recherches nécessaires pour trouver comment copier un fichier dans un autre, le lire, mais je bloque au moment de devoir remplacer les variables placées dans le fichier.
Voici le bout de code que j'ai pu commencer à faire:
Le changement est fait au niveau des deux chaines de caractères mais la modification n'est pas faite au moment où le programme écrit dans le fichier.
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 import java.io.*; import java.util.Scanner; class FichierStreamTest { public static void main(String[] args) { String fichier ="jconf_aten.txt"; String client = "@Client@"; String ligne; try { File inputFile = new File("jconf_aten.txt"); File outputFile = new File("Jconf_copie.txt"); FileInputStream fis = new FileInputStream(inputFile); FileOutputStream fos = new FileOutputStream(outputFile); try{ InputStream ips=new FileInputStream(fichier); InputStreamReader ipsr=new InputStreamReader(ips); BufferedReader br=new BufferedReader(ipsr); Scanner sc = new Scanner(System.in); System.out.println("Nom du client :"); String str = sc.nextLine(); while ((ligne=br.readLine())!=null){ if (ligne.contains(client)) { int index = ligne.indexOf(client) ; String ext = ligne.substring(index, index+8); System.out.println(ligne); int a; while ((a = fis.read()) != -1) { String tmp; int lgFind= ext.length();; for (int k=0;k<(ligne.length());k++) { try { tmp = ligne.substring(k,k+lgFind); } catch(Exception e) { break; } if ( tmp.equalsIgnoreCase(ext) ) { ext = ligne.substring(0,k) + str + ligne.substring(k+lgFind,ligne.length()); } } System.out.println(ext); fos.write(a); } } } sc.close(); br.close(); }finally{} fis.close(); fos.close(); } catch (FileNotFoundException e) { System.err.println("FileStreamsTest: " + e); } catch (IOException e) { System.err.println("FileStreamsTest: " + e); } }
Je suis débutant en programmation et donc en java alors je vous prie d'être indulgent sur les éventuels aberrations que vous pourriez rencontrez
Puisse la lumière de votre savoir atteindre les portes de mon cerveau.
Partager