Ticket #2322: do_not_destroy_prefs.patch
| File do_not_destroy_prefs.patch, 2.9 KB (added by , 17 years ago) |
|---|
-
src/org/openstreetmap/josm/data/Preferences.java
12 12 import java.io.InputStreamReader; 13 13 import java.io.OutputStreamWriter; 14 14 import java.io.PrintWriter; 15 import java.nio.channels.FileChannel; 15 16 import java.util.ArrayList; 16 17 import java.util.Arrays; 17 18 import java.util.Collection; … … 43 44 public class Preferences { 44 45 45 46 /** 46 * Internal storage for the preference ddirectory.47 * Internal storage for the preference directory. 47 48 * Do not access this variable directly! 48 49 * @see #getPreferencesDirFile() 49 50 */ … … 290 291 properties.put("josm.version", AboutAction.getVersionString()); 291 292 try { 292 293 setSystemProperties(); 294 String prefFile = getPreferencesDir() + "preferences"; 295 296 // Backup old preferences 297 copyFile(new File(prefFile), new File(prefFile + "_backup")); 298 293 299 final PrintWriter out = new PrintWriter(new OutputStreamWriter( 294 new FileOutputStream( getPreferencesDir() + "preferences"), "utf-8"), false);300 new FileOutputStream(prefFile + "_tmp"), "utf-8"), false); 295 301 for (final Entry<String, String> e : properties.entrySet()) { 296 302 String s = defaults.get(e.getKey()); 297 303 /* don't save default values */ … … 300 306 } 301 307 } 302 308 out.close(); 309 310 File tmpFile = new File(prefFile + "_tmp"); 311 copyFile(tmpFile, new File(prefFile)); 312 tmpFile.delete(); 313 303 314 } catch (final IOException e) { 304 315 e.printStackTrace(); 305 316 // do not message anything, since this can be called from strange … … 307 318 } 308 319 } 309 320 321 /** 322 * Simple file copy function that will overwrite the target file 323 * Taken from http://www.rgagnon.com/javadetails/java-0064.html (CC-NC-BY-SA) 324 * @param in 325 * @param out 326 * @throws IOException 327 */ 328 public static void copyFile(File in, File out) throws IOException { 329 FileChannel inChannel = new FileInputStream(in).getChannel(); 330 FileChannel outChannel = new FileOutputStream(out).getChannel(); 331 try { 332 inChannel.transferTo(0, inChannel.size(), 333 outChannel); 334 } 335 catch (IOException e) { 336 throw e; 337 } 338 finally { 339 if (inChannel != null) { 340 inChannel.close(); 341 } 342 if (outChannel != null) { 343 outChannel.close(); 344 } 345 } 346 } 347 348 310 349 public void load() throws IOException { 311 350 properties.clear(); 312 351 final BufferedReader in = new BufferedReader(new InputStreamReader(
