Bonjour,
Comment mettre à jour un fichier .properties en java,tout en gardant les commentaires (Lignes commençant par un #). Merci
[ Modéré par Bulbo ]
Ajout d'un tag dans le titre
Les Règles du Forum
Bonjour,
Comment mettre à jour un fichier .properties en java,tout en gardant les commentaires (Lignes commençant par un #). Merci
[ Modéré par Bulbo ]
Ajout d'un tag dans le titre
Les Règles du Forum
Il faut que tu commences par ouvrir ton fichier en lecture.
Tu peux ensuite lire ton fichier ligne par ligne et effectuer toutes les mises à jours et modification que tu veux
Le probleme c'est que la classe Properties ne sauve pas les commentaires du fichier qui a servi a la remplir
Ce que j'ai fait pour pallier a ca, c'est une soluce comme bob te propose ..
Je lis le fichier ligne par ligne, si ma ligne commence par # je l'ecrit tout simplement, si elle commence par une cle, je remplace la ligne par le couple cle/valeur present dans la classe Properties ..
Les cles que je n'ai pas rencontrees dans le fichier, je les ajoute a la fin ..
Bulbo
Si cela peux t'aider j'ai déjà codé une petite classe utilitaire pour faire des merges de fichiers de propriétés.
Si cela peut aider voici le code des classes que j'utilise
PropertiesMerger:
SearchAndReplace:
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 package ...; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Enumeration; import java.util.Properties; import java.util.regex.Pattern; import org.apache.log4j.Logger; /** * Created on 20 sept. 2006. * * @author nicgando<br> * Take two properties files and merge them */ public class PropertiesMerger { private static final Logger logger = Logger.getLogger(PropertiesMerger.class); private DateFormat svgFormat = new SimpleDateFormat("yyyyMMdd"); private File file1; private File file2; private File output; /** * @param file1 must exist * @param file2 must exist * @param output could not exist; if exist will be overwritten (a backup is * done: *.save_yyyyMMdd) * @throws IOException */ public PropertiesMerger(File file1, File file2, File output) throws IOException { this.file1 = file1; this.file2 = file2; this.output = output; if (!this.file1.exists() || !this.file1.canRead()) { logger.error("file1: '" + this.file1 + "' doesn't exist or could not be read"); throw new IOException("file1: '" + this.file1 + "' doesn't exist or could not be read"); } if (!this.file2.exists() || !this.file2.canRead()) { logger.error("file2: '" + this.file2 + "' doesn't exist or could not be read"); throw new IOException("file2: '" + this.file2 + "' doesn't exist or could not be read"); } if (this.output.exists()) { logger.warn("output file: '" + this.output + "' exist"); File bckFile = new File(this.output.getAbsoluteFile() + ".save_" + this.svgFormat.format(new Date())); logger.warn("rename file (" + this.output + ") into " + bckFile); if (bckFile.exists()) { logger.warn(bckFile.getAbsoluteFile() + " exist: delete it"); bckFile.delete(); logger.error("could not delete: '" + bckFile); this.output.renameTo(bckFile); } } else { if (!this.output.createNewFile()) { logger.error("could not create: '" + this.output); throw new IOException("could not create: '" + this.output); } } } private Properties loadProperties(File f) throws IOException { logger.debug("load " + f.getAbsoluteFile() + " properties"); Properties props = new Properties(); FileInputStream in = new FileInputStream(f); props.load(in); FileUtilities.closeStream(in); logger.debug("properties are: " + props); return props; } private void propertiesSpread(Properties p1, Properties p2, Properties spread) { Enumeration enumerator = p1.propertyNames(); while (enumerator.hasMoreElements()) { String key = (String) enumerator.nextElement(); String value = p2.getProperty(key); if (value == null) { spread.setProperty(key, p1.getProperty(key)); } } } /** * Load the file1 and file2 properties files and compare their properties: * <br> * <li>All properties from the file1 miss in file2 are copy in the output * file</li> * <li>All properties from the file2 miss in file1 are copy in the output * file</li> * <li>All properties present in the file1 and file2 are taken from the file2 * </li> * * @throws IOException */ public void merge() throws IOException { Properties file1Props = this.loadProperties(this.file1); Properties file2Props = this.loadProperties(this.file2); logger.debug("search source properties which are not in the target properties ..."); logger.debug("override target properties with source properties ..."); Properties spread = new Properties(); //optionnal because we will copy file1 into the output //this.propertiesSpread(file1Props, file2Props, spread); this.propertiesSpread(file2Props, file1Props, spread); logger.debug("spread between file2 and file1 is: " + spread); logger.debug("copy the file1 properties in the output file"); FileUtilities.copy(this.file1, this.output); Enumeration enumerator = file2Props.propertyNames(); while (enumerator.hasMoreElements()) { String key = (String) enumerator.nextElement(); String file2Value = file2Props.getProperty(key); String file1Value = file1Props.getProperty(key); if (!file2Value.equalsIgnoreCase(file1Value)) { Pattern pattern = Pattern.compile("\\s*" + key + "\\s*=\\s*" + file1Value + "\\s*"); String replace = "\n#modify (" + this.svgFormat.format(new Date()) + ") : '" + key + "=" + file1Value + "'\n" + key + "=" + file2Value; SearchAndReplace.execute(this.output, pattern, replace); } } if (!spread.isEmpty()) { logger.debug("add the poperties spread in the target file"); FileOutputStream out = new FileOutputStream(this.output, true); out.write(("\n\n\n#--- (" + this.svgFormat.format(new Date()) + ") added ---\n").getBytes()); enumerator = spread.propertyNames(); while (enumerator.hasMoreElements()) { String key = (String) enumerator.nextElement(); String value = spread.getProperty(key); String line = key + "=" + value; logger.info("added: '" + line + "' into the output file"); out.write((line + "\n").getBytes()); } FileUtilities.closeStream(out); } logger.info("done"); } }
et FileUtilities
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 package ...; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.log4j.Logger; /** * Created on 25 sept. 2006. * * @author nicgando */ public class SearchAndReplace { private static final Logger logger = Logger.getLogger(SearchAndReplace.class); public static void execute(File srcFile, Pattern search, String replace) { logger.info("search '" + search.pattern() + "' and replace by '" + replace + "' in " + srcFile); InputStream ins = null; InputStreamReader inr = null; BufferedReader br = null; OutputStream outs = null; OutputStreamWriter outw = null; BufferedWriter bw = null; try { logger.debug("create tmp file ..."); File tmpFile = new File("searchNreplace.tmp"); if (!tmpFile.exists() && !tmpFile.createNewFile()) { throw new IOException("could not create the tmp file: " + tmpFile); } logger.debug("create tmp file done " + tmpFile); outs = new FileOutputStream(tmpFile, false); outw = new OutputStreamWriter(outs); bw = new BufferedWriter(outw); ins = new FileInputStream(srcFile); inr = new InputStreamReader(ins); br = new BufferedReader(inr); logger.debug("start search and replace ..."); String line = br.readLine(); String result = ""; while (line != null) { Matcher m = search.matcher(line); result = m.replaceAll(replace); bw.write(result + "\n"); line = br.readLine(); } logger.debug("start search and replace done"); logger.debug("close all streams"); FileUtilities.closeStream(br); FileUtilities.closeStream(inr); FileUtilities.closeStream(ins); FileUtilities.closeStream(bw); FileUtilities.closeStream(outw); FileUtilities.closeStream(outs); logger.debug("rename " + tmpFile); if (!srcFile.delete()) { throw new IOException("could not delete " + srcFile); } if (!tmpFile.renameTo(srcFile)) { throw new IOException("could not rename " + tmpFile + " to " + srcFile); } } catch (IOException ioe) { logger.error(ioe.getMessage(), ioe); logger.error("close all streams"); FileUtilities.closeStream(ins); FileUtilities.closeStream(inr); FileUtilities.closeStream(br); FileUtilities.closeStream(outs); FileUtilities.closeStream(outw); FileUtilities.closeStream(bw); } } }
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 package ...; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; /** * Created on 25 sept. 2006. * * @author nicgando<br/> */ public class FileUtilities { public static void closeStream(InputStream in) { if (in != null) try { in.close(); } catch (IOException ioe) { //nothing todo } } public static void closeStream(BufferedReader br) { if (br != null) try { br.close(); } catch (IOException ioe) { //nothing todo } } public static void closeStream(InputStreamReader inr) { if (inr != null) try { inr.close(); } catch (IOException ioe) { //nothing todo } } public static void closeStream(OutputStream out) { if (out != null) { try { out.flush(); out.close(); } catch (IOException ioe) { //nothing todo } } } public static void closeStream(OutputStreamWriter outw) { if (outw != null) try { outw.flush(); outw.close(); } catch (IOException ioe) { //nothing todo } } public static void closeStream(BufferedWriter bw) { if (bw != null) try { bw.flush(); bw.close(); } catch (IOException ioe) { //nothing todo } } public static void copy(File src, File tgt) throws IOException { InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(tgt); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } FileUtilities.closeStream(in); FileUtilities.closeStream(out); } }
Partager