private void saveProperties ( ) { assert Thread . holdsLock ( properties ) ; try { // Create a properties instance that sorts the output by keys (case-insensitive) Properties writer = new Properties ( ) { private static final long serialVersionUID = 6953022173340009928L ; @ Override public Enumeration < Object > keys ( ) { SortedSet < Object > sortedSet = new TreeSet <> ( Collator . getInstance ( Locale . ROOT ) ) ; Enumeration < Object > e = super . keys ( ) ; while ( e . hasMoreElements ( ) ) sortedSet . add ( e . nextElement ( ) ) ; return Collections . enumeration ( sortedSet ) ; } } ; writer . putAll ( properties ) ; File tmpFile = File . createTempFile ( "ApplicationResources" , null , sourceFile . getParentFile ( ) ) ; OutputStream out = new BufferedOutputStream ( new FileOutputStream ( tmpFile ) ) ; try { // Write any comments from when file was read if ( sourceFileComments != null ) { for ( String line : sourceFileComments ) { out . write ( line . getBytes ( propertiesCharset ) ) ; out . write ( EOL . getBytes ( propertiesCharset ) ) ; } } // Wrap to skip any comments generated by Properties code out = new SkipCommentsFilterOutputStream ( out ) ; writer . store ( out , null ) ; } finally { out . close ( ) ; } FileUtils . renameAllowNonAtomic ( tmpFile , sourceFile ) ; } catch ( IOException err ) { throw new RuntimeException ( err ) ; } }
Saves the properties file in ascending key order . All accesses must already hold a lock on the properties object .